字符指针的函数参数 [英] char pointer as function parameter
本文介绍了字符指针的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有,有一个的char *
参数功能的。在 A()
,我用的sizeof
来计算BUF的大小,但我的i686机和4得到8在阿梅尔机。为什么不是128?该片段是如下:
I have a function A that has a char *
parameter. In A()
, I use sizeof
to calculate the size of buf , but I got 8 in i686 machine and 4 in armel machine. why is not it 128? The snippet is as below:
void A(char *p)
{
printf("sizeof p is %d\n", sizeof(p));
}
int main(void)
{
char buf[128];
printf("sizeof buf is %d\n", sizeof(buf));
A(buf);
return 0;
}
结果是这样的(在i686的):
the result is like this(in i686):
sizeof buf is 128
sizeof p is 8
请告诉我原因。
推荐答案
由于您打印的指针的大小,而不是它指向。数组衰变为指针,只要你把它传递给一个函数,你有一个指针代替,而失去原来的阵列的所有大小的信息。
Because you print the size of the pointer, and not what it points to. Arrays decays to pointers, and as soon as you pass it to a function you have a pointer instead, and loose all size information of the original array.
这篇关于字符指针的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文