Producing uppercase letters without pointers

前端 未结 2 523
南笙
南笙 2021-01-25 01:12

I am trying to write a function, uppercase, that converts all lowercase characters in a string into their uppercase equivalents.

However, I am getting a Bus 10 error in

2条回答
  •  耶瑟儿~
    2021-01-25 01:32

    The reason you get a crash is that your code modifies a string literal. Characters inside string literals are placed in protected memory area, and therefore may not be changed: it us undefined behavior.

    Replace this

    uppercase("cold");
    

    with this:

    char cold[] = "cold";
    uppercase(cold);
    

    Now the characters of the string are placed in a modifiable area of memory, allowing you to make changes as needed.

提交回复
热议问题