Cannot understand return value of sizeof()

后端 未结 5 508
南旧
南旧 2020-12-11 08:59

I have this in my code:

int x = 4;
char* array = malloc(x*sizeof(char));
size_t arraysize = sizeof (array);
printf(\"arraysize: %zu\\n\", arraysize);
         


        
5条回答
  •  悲&欢浪女
    2020-12-11 09:24

    sizeof doesn't have a return value because it isn't a function, it's a C language construct -- consider the fact that you can write sizeof array without the parentheses. As a C language construct, its value is based entirely on compile-time information. It has no idea how big your array is, only how big the array pointer variable is. See http://en.wikipedia.org/wiki/Sizeof for complete coverage of the subject.

提交回复
热议问题