1

Possible Duplicate:
Why does simple C code receive segmentation fault?

I have code below which will remove trailing spaces from a string but i don't what going in this code so as it gives segmentation fault problem??

 void main(void);

char* rtrim(char*);

void main(void)
 { 
 char* trail_str = "This string has trailing spaces in it.               ";


 printf("Before calling rtrim(), trail_str is '%s'\n", trail_str);

 printf("and has a length of %d.\n", strlen(trail_str));



 rtrim(trail_str);



 printf("After calling rtrim(), trail_str is '%s'\n", trail_str);

 printf("and has a length of %d.\n", strlen(trail_str));

 }


  char* rtrim(char* str)
  {
   int n = strlen(str) - 1;    

    while (n>0)            
   {
       if (*(str+n) != ' ')    
      {

           *(str+n+1) = '\0'; 



           break;             
      }

      else

      n--;
 }

 return str;      

 }
Community
  • 1
  • 1
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • char trail_str[] = "This string has trailing spaces in it. "; Duplicates http://stackoverflow.com/questions/164194/why-does-simple-c-code-receive-segmentation-fault – Gilbert Jun 19 '11 at 11:58

2 Answers2

3

trail_str points to a constant area in the memory and thus cannot be changed in *(str+n+1) = '\0'

when initializing

char* trail_str = "This string has trailing spaces in it.               ";

you actualy generate a constant string: "This string has trailing spaces in it. " and tell trail_str to point to it.

amit
  • 175,853
  • 27
  • 231
  • 333
  • r u saying declaring like char trail_str[]=""This string has trailing spaces in it. "; will solve this problem?? – Amit Singh Tomar Jun 19 '11 at 12:02
  • (ironic: @amit is answering @AMIT): yes, because str[]="..." is actually str[] = { 'T','h',...} (creating array of chars on stack and initializing it) while *str = "..." creates a const char[] on memory, and points to it. – amit Jun 19 '11 at 12:06
  • Thanks @AMIT i GOT your point.but still am not getting desired result ,may be logic have some problem. – Amit Singh Tomar Jun 19 '11 at 12:09
  • note that for a string containing only whitespaces `(" ")` the function will give seg fault, because `n` will be negative! (you will not enter the if condition and break, and try to approach str-1, which will result in undefined behavior.) – amit Jun 19 '11 at 12:26
2
char* trail_str = "This string has trailing spaces in it.               ";

The string pointed to by tail_str can be stored in read-only memory. You can't modify it.

If you want to modify it, you'll need to allocate storage for it and copy that string constant .

(Also, main should return an int, not void.)

Mat
  • 202,337
  • 40
  • 393
  • 406
  • The type of `"blablabla"` is `const char[]` which decays to `const char*`, not `char*`. – rubenvb Jun 19 '11 at 11:59
  • 2
    @rubenvb: not in C. The type of a string literal is `char[]` (and string literals decay to type `char*`). It is, however, undefined behaviour to modify the contents of a literal string. Some people like to **add** `const` when declaring pointers to string literals and get help from the compiler in case they try to modify it – pmg Jun 19 '11 at 12:41
  • Then what does C99 6.4.5/5: `The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence` and 6.7.8/4: `All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.` mean? One references the other, and I can't make out if the char* array of static storage duration used for the string literal will need to be a constant expression. This is confusing wording... – rubenvb Jun 19 '11 at 13:34