sizeof char* array in C/C++

后端 未结 6 716
旧巷少年郎
旧巷少年郎 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:26

    The sizeof of an element returns the size of the memory allocated for that object. In your example the string is probably declared somewhere as

    samplestring[4]
    

    In which case the size of the memory is 4. The method you probably want in your application is

    strlen(samplestring);
    

    Which returns the size of the null terminated string (without the termination)

提交回复
热议问题