【发布时间】:2013-02-18 10:02:06
【问题描述】:
void myfunc(char* passedArray)
{
cout << sizeof(passedArray) << endl; // prints 4
}
int main(void)
{
char charArray[] = "abcdefghiklmop";
cout << sizeof(charArray) << endl; // prints 15
myfunc(charArray);
cout << sizeof(charArray) << endl; // prints 15
}
我相信它仍然应该在该函数中打印 15...
【问题讨论】:
-
这个问题已经被问过很多次了。你试过用谷歌搜索吗?
-
是的,彻底。它不在 SO 上,我在任何论坛上都找不到。
-
你传入了一个参数指针,
sizeof(char*)==4偶然为你的平台持有。基本上没有办法确定数组从指针指向第一个元素的长度。 -
那么不仅这个问题是失败的,你的谷歌搜索技能似乎也缺乏。 stackoverflow.com/questions/4941142/c-sizeof-string-is-always-8/stackoverflow.com/questions/1392200/sizeof-string-literal/…stackoverflow.com/questions/11919369/…stackoverflow.com/questions/1392200/sizeof-string-literal
-
我建议下次有人发布与
c++arraysizeof相关主题的帖子时,请在他们点击“发布”之前显示此帖子列表。
标签: c++ arrays parameter-passing sizeof