Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 2.14 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. concatanation char with char *
  2. Ex:
  3.  
  4.     char* foo ( char x, char * xc ) {
  5.  
  6.  
  7.         xc =  realloc ( xc, 1 + strlen ( xc ) ) ;
  8.         strcat ( xc, x ) ;
  9.  
  10.     return xc ;
  11.     }
  12.  
  13.                              p =  heap variable
  14.    foo ( 'a', NULL ) ==>     ------------
  15.                              | 'a'| ''|
  16.                              ------------
  17.  
  18.    foo ( 'b', p )    ===>    --------------------
  19.                              | 'a' | 'b' | '' |
  20.                              --------------------
  21.  
  22.    foo ( 'c', p )    ===>    --------------------------
  23.                              | 'a' | 'b' | 'c' | '' |
  24.                              --------------------------
  25.        
  26. size_t len = xc != NULL ? strlen(xc) : 0;
  27. xc = realloc(xc, len + 1 + 1);
  28. xc[len] = c;
  29. xc[len + 1] = '';
  30.        
  31. len =strlen(xc);
  32. xc =  realloc ( xc, 2 + strlen ( xc ) ) ; //One for NULL character
  33.  
  34. sprintf(xc+len,"%c", x);
  35.        
  36. int len = 2;
  37. if ( xc != NULL ) {
  38.    len += strlen( xc );
  39. }
  40. xc = realloc( xc, len );
  41. *(xc+len-2) = x;
  42. *(xc+len-1) = '';
  43.        
  44. char* append_char(char c, char * str)
  45. {
  46.     char* new_str;
  47.  
  48.     if(NULL != str) {
  49.         new_str = realloc(str, strlen(str) + 2);
  50.         strncpy(new_str, str, strlen(str));
  51.         new_str[strlen(str)] = c;
  52.         new_str[strlen(str)+1] = '';
  53.     } else {
  54.         new_str = malloc(2);
  55.         new_str[0] = c;
  56.         new_str[1] = '';
  57.     }
  58.  
  59.     return new_str;
  60. }
  61.        
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65.  
  66. int sl = 0; // sl for strlen(s), allocated memory size will be +1 byte
  67. char* s = NULL;
  68.  
  69. char* ccat(char* s, char c) {
  70.     if (s == NULL) sl = 1; // allocating new memory
  71.     else           sl = strlen(s) + 1; // +1 for new char
  72.     // realloc will work like malloc if s is NULL
  73.     s = realloc(s, sl+1); // +1 for zero at the end of string
  74.     if(s == NULL) {
  75.         fprintf(stderr, "realloc error");
  76.         exit(1);
  77.     }
  78.     s[sl-1] = c;
  79.     s[sl] = '';
  80.     return s;
  81. }
  82.  
  83. int main(int argc, char** argv) {
  84.     s = ccat(s, 'Y');
  85.     printf("sl=%d s=%sn", sl, s);
  86.  
  87.     s = ccat(s, 'P');
  88.     printf("sl=%d s=%sn", sl, s);
  89.  
  90.     s = ccat(s, 'A');
  91.     printf("sl=%d s=%sn", sl, s);
  92.  
  93.     if (s != NULL)
  94.         free(s);
  95.     exit(0);
  96. }