0

I want to pass string to a c function using pointer to char and modify it and it gave me segmentation fault. I don't know why ? Note*: I know I can pass the string to array of character will solve the problem

I tried to pass it to array of character and pass to function the name of array and it works , but I need to know what the problem of passing the pointer to character.

void convertToLowerCase(char* str){

    int i=0;
    while(str[i] != '\0')
    {
        if(str[i]>='A'&& str[i]<='Z'){
            str[i]+=32;
        }
        i++;
    }
}

int main(void){

    char *str = "AHMEDROSHDY";
    convertToLowerCase(str);
}

I expect the output str to be "ahmedroshdy", but the actual output segmentation fault

2 Answers2

3

This (you had char str* which is a syntax error, fixed that):

char *str = "AHMEDROSHDY";

is a pointer to a string literal, thus it cannot be modified, since it is stored in read-only memory.

You modify it here str[i]+=32;, which is not allowed.

Use an array instead, as @xing suggested, i.e. char str[] = "AHMEDROSHDY";.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • you means this as const char* str ? I can't get why this happens ? – ahmed roshdy May 12 '19 at 13:46
  • 2
    Because it stored in read-only memory @ahmedroshdy. Check [this](https://stackoverflow.com/questions/5464183/modifying-string-literal) for more. – gsamaras May 12 '19 at 13:46
  • 1
    @xing it is [platform-specific](https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go) where string literals are stored. – gsamaras May 12 '19 at 13:51
  • @gsamaras it is compiler dependent or what ? that means this value allocated in memory after linkage (linker process) I mean not in compile time right ? but this why happen ? – ahmed roshdy May 12 '19 at 13:57
  • 1
    Read the link @ahmedroshdy I answered with to xing. Because this is how the language is designed. Hope that helps! :) – gsamaras May 12 '19 at 13:58
  • @gsamaras yes it is helpful also [This](https://stackoverflow.com/questions/5464183/modifying-string-literal) , sorry for asking more but I like to deep understand this. anyway to enclose this we can say that any pointer to character for any platform it must be in data segment and this is how the language designed right ?.. thanks – ahmed roshdy May 12 '19 at 14:07
  • 1
    Any string literal is designed by the language to be not modifiable @ahmedroshdy. – gsamaras May 12 '19 at 14:17
  • 1
    Any string literal is designed by the language to be not modifiable @ahmedroshdy. BTW, it would be nice to accept an answer from the two posted here, since both are correct. – gsamaras May 12 '19 at 14:25
1

To be more precise:

char *str = "AHMEDROSHDY";

'str` is a pointer to the string literal. String literals in C are not modifacable

in the C standard:

The behavior is undefined in the following circumstances: ...

  • The program attempts to modify a string literal
Community
  • 1
  • 1
0___________
  • 60,014
  • 4
  • 34
  • 74