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

大兔子大兔子 提交于 2019-11-30 10:00:11

问题


Consider the following code

#include <stdio.h>

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

int main(){
 char string[] = "Hello World";
 print(string);
}

and the output is

Hello World:4

So what's wrong with that ?


回答1:


It does return the true size of the "variable" (really, the parameter to the function). The problem is that this is not of the type you think it is.

char string[], as a parameter to a function, is equivalent to char* string. You get a result of 4 because that is the size, on your system, of a char*.

Please read more here: http://c-faq.com/aryptr/index.html




回答2:


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

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




回答3:


string is a pointer and its size is 4. You need strlen probably.




回答4:


a array will change into a pointer as parameter of function in ANSI C.




回答5:


Except when it is an operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an array expression will have its type implicitly converted ("decay") from "N-element array of T" to "pointer to T" and its value will be the address of the first element in the array (n1256, 6.3.2.1/3).

The object string in main is a 12-element array of char. In the call to print in main, the type of the expression string is converted from char [12] to char *. Therefore, the print function receives a pointer value, not an array. In the context of a function parameter declaration, T a[] and T a[N] are both synonymous with T *; note that this is only true for function parameter declarations (this is one of C's bigger misfeatures IMO).

Thus, the print function is working with a pointer type, not an array type, so sizeof string returns the size of a char *, not the size of the array.




回答6:


A string in c is just an array of characters. It isn't necessarily NUL terminated (although in your case it is). There is no way for the function to know how long the string is that's passed to it - it's just given the address of the string as a pointer.

"String" is that pointer and on your machine (a 32 bit machine) it takes 4 bytes to store a pointer. So sizeof(string) is 4




回答7:


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.




回答8:


http://www.java2s.com/Code/Cpp/Data-Type/StringSizeOf.htm see here it has same output as yours...and find what ur doing wrong



来源:https://stackoverflow.com/questions/4366600/sizeof-doesnt-return-the-true-size-of-variable-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!