0

I have the following function in C:

int TransMinMaj(char *c)
{
    if((*c) >= 'a' && (*c) <= 'z')
    {
        *c += 'A' - 'a';
        return 1;
    }
    else if((*c) >= 'A' && (*c) <= 'Z')
        return 1;
    return 0;

}

As you can see, this function:
1. Return 1 if the character tested is a letter
2. Transform the lower case to upper case
3. Otherwise, return 0 (to indicate that it is not a letter)

I have chosen to pass the parameter by address because I want to change the value of the passed parameter c in the memory.

Now my question comes: I tested my function withe the following code :

char str[] = "abcdefg";
printf("Before: %s\n", str);    
TransMinMaj(&str[1]);
printf("After: %s\n", str);

Until now, everything is good, no errors, no warnings. And as you can see, in this test, I transforme the lower case letter b (also the second element of the string (array of characters)) to the upper case letter 'B' in the memory.

But, if change the test code a litte bit:

char *str = "abcdefg";
printf("Before: %s\n", str);    
TransMinMaj(&str[1]);
printf("After: %s\n", str);

There is a segmentation error(core dumped). But I don't know why.
So here comes my question:
1. What is the difference between char *str and char str[] in a declaration ?
2. Where does my segmentation error come from?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
SWIIWII
  • 387
  • 5
  • 15
  • Please use search before asking questions. It is very likely that your question has been asked before. This one has been asked since the beginning of time. – 2501 Sep 09 '16 at 07:38
  • With the pointer you can do: ptr1=ptr2; with the array not. With the pointer you can do ptr1++; with the array not. With the array you can call sizeof(array)/sizeof(array[0]) with the pointer not. And such things – jurhas Sep 09 '16 at 07:51

1 Answers1

2
char str[] = "abcdefg";

and

char *str = "abcdefg";

are two different things.

  • The first one is an array, initialized with "abcdefg".

  • The second one is a pointer pointing to the string literal "abcdefg".

In the second case, when you attempt to modify the string literal, you invoke undefined behavior, as any attempt to modify a string literal is UB.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261