C/C++ Char Pointer Crash

前端 未结 7 466
甜味超标
甜味超标 2020-12-19 17:42

Let\'s say that a function which returns a fixed ‘random text’ string is written like

char *Function1()
{ 
return “Some text”;
}

then the p

7条回答
  •  执笔经年
    2020-12-19 18:12

    The question shows that you do not understand the string literals.

    image this code

    char* pch = "Here is some text";
    char* pch2 = "some text";
    char* pch3 = "Here is";
    

    Now, how the compiler allocates memory to the strings is entirely a matter for the compiler. the memory might organised like this:

    Here isHere is some text
    

    with pch2 pointing to memory location inside the pch string.

    The key here is understanding the memory. Using the Standard Template Library (stl) would be a good practice, but you may be quite a steep learning curve for you.

提交回复
热议问题