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

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 0.59 KB  |  hits: 12  |  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. Replace a character in a char[] from a function
  2. int main (void)
  3. {  
  4.   char* string = "Hello World!";
  5.  
  6.   printf ("%sn", string);
  7.   replace_char(string, 10, 'a');
  8.   printf ("%sn", string);
  9. }
  10.  
  11. void replace_char(char str[], int n, char c)
  12. {
  13.   str[n] = c;
  14. }
  15.        
  16. void replace_char(char*& str, int n, char c)
  17. {
  18.   str = strdup(str);
  19.   str[n] = c;
  20. }
  21.  
  22. int main()
  23. {
  24.     char* string = "Hello World!";
  25.     string = replace_char(string, 10, 'a');
  26.  
  27.     // ...
  28.     free(string);
  29. }
  30.        
  31. const char* conststr = "Hello World!";
  32. char * string = strdup(conststr);
  33.        
  34. gcc -Wall test.c
  35.        
  36. char string[] = "Hello World!";