Sizeof doesn't return the true size of variable in C

后端 未结 8 1061
醉梦人生
醉梦人生 2020-12-22 10:47

Consider the following code

#include 

void print(char string[]){
 printf(\"%s:%d\\n\",string,sizeof(string));
}

int main(){
 char string[] =         


        
相关标签:
8条回答
  • 2020-12-22 11:17

    You asked the systems for the sizeof(the address to the begining of a character array), string is an object, to get information about it's lenght out you have to ask it through the correct OO interface.

    In the case of std::string the member function string.length(), will return the number of characters stored by the string object.

    0 讨论(0)
  • 2020-12-22 11:20

    It is the size of the char pointer, not the length of the string.

    Use strlen from string.h to get the string length.

    0 讨论(0)
提交回复
热议问题