在函数原型的参数 [英] Arguments in a function prototype
问题描述
我的问题是:当我写在C函数原型是这样的:
My question is: when i write a function prototype in C like this:
void foo(int *vector);
这是应该做同样的事情:
It's the same thing to do:
void foo(int vector[MAX_LENGTH]);
要的功能,始终被传递为指针?在code这是一样的吗?
先谢谢了。
To the function, is passed always as a pointer? The code it's the same? Thanks in advance.
推荐答案
这是微妙的。在C数组不是指针,但C不会让数组作为函数的参数传递。所以,当你有无效美孚(INT矢量[MAX_LENGTH]);
,基本上所有你做的是告诉其他程序员(和你的未来的自己),这个函数需要一个数组 MAX_LENGTH的
要传递给它。编译器不会帮你的。它会悄悄地蒙上你的数组的指针。
This is subtle. Arrays in C are not pointers, but C does not allow arrays to be passed as function parameters. So when you have void foo(int vector[MAX_LENGTH]);
, essentially all you're doing is telling other programmers (and your future self) that this function expects an array of MAX_LENGTH
to be passed to it. The compiler won't help you. It will silently cast your array to a pointer.
这解释它$ P $ ptty好。
This explains it pretty well.
这篇关于在函数原型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!