Implementation of ToLower function in C

前端 未结 4 1133
不知归路
不知归路 2021-01-27 08:28

I am writing my own implementation of ToLower(char *str) in C. But i am getting segmentation fault in the function. The function which i wrote is :

void ToLower(         


        
4条回答
  •  心在旅途
    2021-01-27 09:24

    You are almost certainly failing when you call it like:

    int main(void)
    {
        ToLower("HelloWorld");
        return 0;
    }
    

    This is because "HelloWorld" is a literal, constant string, and you cannot change its contents.

    Try instead:

    int main(void)
    {
        char str[] = "HelloWorld";
    
        // Now str is your own local buffer, that you can modify.
        // It is initialized with the text, but that text can be changed.
        ToLower(str);
        return 0;
    }
    

提交回复
热议问题