关于sizeof

前连天在迈普的一面上,面试官问了一个关于sizeof的问题

char * str="abc";

char str1[]="abcde";

sizeof(str)==?//4

sizeof(str1)=?//6

今天在诚迈的笔试中也出现了同样的问题

在这里总结一下

When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof.

这是msdn里的解释。

对指针来说,返回指针的字节数

对数组来说,返回数组所占的所有字节

你可能感兴趣的:(sizeof)