sizeof char* array in C/C++

后端 未结 6 708
旧巷少年郎
旧巷少年郎 2021-02-14 07:57

There are plenty of similar inquires, but in my case I don\'t understand what isn\'t working:

int mysize = 0;
mysize = sizeof(samplestring) / sizeof(*samplestrin         


        
6条回答
  •  长发绾君心
    2021-02-14 08:19

    4 isn't the size of the string, because samplestring isn't a string. It's a char*, whose size is (on your platform) 4, divided by 1 (size of char) is, correctly, 4.

    In C++, you'd use std::string and the length() method.

    In C, you'd use strlen which takes as parameter a NULL-terminated char pointer.

提交回复
热议问题