Sizeof 不返回 C 中变量的真实大小
考虑下面的代码
#include <stdio.h>
void print(char string[]){
printf("%s:%d\n",string,sizeof(string));
}
int main(){
char string[] = "Hello World";
print(string);
}
,输出是
Hello World:4
那么有什么问题吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它确实返回“变量”的真实大小(实际上是函数的参数)。问题是这不是你想象的那种类型。
char string[]
作为函数的参数,相当于char* string
。您得到的结果为4
,因为这是您系统上char*
的大小。请在此处阅读更多信息:http://c-faq.com/aryptr/index.html
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 tochar* string
. You get a result of4
because that is the size, on your system, of achar*
.Please read more here: http://c-faq.com/aryptr/index.html
它是字符指针的大小,而不是字符串的长度。
使用 string.h 中的 strlen 来获取字符串长度。
It is the size of the char pointer, not the length of the string.
Use strlen from string.h to get the string length.
string
是一个指针,其大小为 4。您可能需要strlen
。string
is a pointer and its size is 4. You needstrlen
probably.在 ANSI C 中,数组将变成指针作为函数的参数。
a array will change into a pointer as parameter of function in ANSI C.
除非它是
sizeof
或一元&
运算符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则数组表达式将具有其类型从“T 的 N 元素数组”隐式转换(“衰减”)为“指向 T 的指针”,其值将是数组中第一个元素的地址 (n1256, 6.3.2.1/3)。main
中的 objectstring
是一个 12 元素的char
数组。在main
中调用print
时,表达式string
的类型由char [ 转换而来12]
到char *
。因此,print
函数接收的是指针值,而不是数组。在函数参数声明的上下文中,T a[]
和T a[N]
都与T *
同义;请注意,这仅对于函数参数声明是正确的(在我看来,这是 C 的较大缺陷之一)。因此,
print
函数使用的是指针类型,而不是数组类型,因此sizeof string
返回char *
的大小,而不是数组的大小。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
inmain
is a 12-element array ofchar
. In the call toprint
inmain
, the type of the expressionstring
is converted fromchar [12]
tochar *
. Therefore, theprint
function receives a pointer value, not an array. In the context of a function parameter declaration,T a[]
andT a[N]
are both synonymous withT *
; 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, sosizeof string
returns the size of achar *
, not the size of the array.c 中的字符串只是一个字符数组。它不一定是 NUL 终止的(尽管在你的情况下是这样)。该函数无法知道传递给它的字符串有多长 - 它只是将字符串的地址作为指针给出。
“String”就是该指针,在您的机器(32 位机器)上,它需要 4 个字节来存储指针。所以 sizeof(string) 是 4
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
您向系统询问了 sizeof(字符数组开头的地址),字符串是一个对象,要获取有关其长度的信息,您必须通过正确的 OO 接口询问它。
对于 std::string 来说,成员函数 string.length() 将返回字符串对象存储的字符数。
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.
http://www.java2s.com/Code/Cpp/Data-Type /StringSizeOf.htm
看到这里它的输出与你的相同......并找出你做错了什么
http://www.java2s.com/Code/Cpp/Data-Type/StringSizeOf.htm
see here it has same output as yours...and find what ur doing wrong