How should I allocate memory for c-string char array?

后端 未结 3 726
再見小時候
再見小時候 2021-01-05 13:19

So in attempting to learn how to use C-Strings in C++, I\'m running into issues with memory allocation.

The idea here is that a new string is created of the format

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 14:01

    sizeof(s1) returns the size of a pointer variable, not the length of the array which it points to. Since you know that s1 points to a C-string, you should use the strlen() function instead.

提交回复
热议问题