在 C 中通过引用传递数组?
如何在 C 中通过引用传递结构体数组?
举个例子:
struct Coordinate {
int X;
int Y;
};
SomeMethod(Coordinate *Coordinates[]){
//Do Something with the array
}
int main(){
Coordinate Coordinates[10];
SomeMethod(&Coordinates);
}
How can I pass an array of structs by reference in C?
As an example:
struct Coordinate {
int X;
int Y;
};
SomeMethod(Coordinate *Coordinates[]){
//Do Something with the array
}
int main(){
Coordinate Coordinates[10];
SomeMethod(&Coordinates);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另请注意,如果您在方法中创建数组,则无法返回它。 如果您返回指向它的指针,则当函数返回时,它将从堆栈中删除。
您必须在堆上分配内存并返回指向该内存的指针。
例如。
also be aware that if you are creating a array within a method, you cannot return it. If you return a pointer to it, it would have been removed from the stack when the function returns.
you must allocate memory onto the heap and return a pointer to that.
eg.
默认情况下,数组通过引用有效传递。 实际上传递的是指向第一个元素的指针的值。 因此,接收此消息的函数或方法可以修改数组中的值。
两次调用是等价的,退出值应该为0;
Arrays are effectively passed by reference by default. Actually the value of the pointer to the first element is passed. Therefore the function or method receiving this can modify the values in the array.
The two calls are equivalent, and the exit value should be 0;
在普通 C 中,您可以在 API 中使用指针/大小组合。
使用指针是最接近 C 语言中的按引用调用的方式。
In plain C you can use a pointer/size combination in your API.
Using pointers is the closest to call-by-reference available in C.
大家好,这里是一个简单的测试程序,展示了如何使用 new 或 malloc 分配和传递数组。 只需剪切、粘贴并运行即可。 玩得开心!
Hey guys here is a simple test program that shows how to allocate and pass an array using new or malloc. Just cut, paste and run it. Have fun!
在 C 语言中,数组作为指向第一个元素的指针传递。 它们是唯一不真正按值传递的元素(指针按值传递,但不复制数组)。 这允许被调用的函数修改内容。
现在,如果您想要更改数组本身(元素数量...),则无法使用堆栈或全局数组来完成此操作,只能使用堆中动态分配的内存。 在这种情况下,如果您想更改指针,则必须传递一个指向它的指针:
在问题编辑中,您专门询问有关传递结构数组的问题。 您有两种解决方案:声明一个 typedef,或者明确您正在传递一个结构:
您可以在声明类型时对其进行 typedef(这是 C 中的常见习惯用法):
In C arrays are passed as a pointer to the first element. They are the only element that is not really passed by value (the pointer is passed by value, but the array is not copied). That allows the called function to modify the contents.
Now, if what you want is changing the array itself (number of elements...) you cannot do it with stack or global arrays, only with dynamically allocated memory in the heap. In that case, if you want to change the pointer you must pass a pointer to it:
In the question edit you ask specifically about passing an array of structs. You have two solutions there: declare a typedef, or make explicit that you are passing an struct:
You can typedef the type as you declare it (and it is a common idiom in C):
要稍微扩展一下此处的一些答案...
在 C 中,当数组标识符出现在上下文中而不是作为 & 的操作数时, 或 sizeof,标识符的类型从“T 的 N 元素数组”隐式转换为“指向 T 的指针”,并且其值隐式设置为数组中第一个元素的地址(与数组本身的地址)。 这就是为什么当您将数组标识符作为参数传递给函数时,该函数会收到指向基本类型的指针,而不是数组。 由于仅通过查看指向第一个元素的指针无法判断数组有多大,因此必须将大小作为单独的参数传递。
有几种将数组传递给函数的替代方法。
存在指向 T 数组的指针,而不是指向 T 的指针。您可以将这样的指针声明为
在本例中,p 是指向 T 的 N 元素数组的指针(而不是 T *p[N],其中 p 是指向 T 的指针的 N 元素数组)。 因此,您可以传递指向数组的指针,而不是指向第一个元素的指针:
此方法的缺点是数组大小是固定的,因为指向 T 的 10 元素数组的指针与指针是不同的类型到包含 T 的 20 个元素的数组。
第三种方法是将数组包装在结构中:
此方法的优点是您无需使用指针。 缺点是数组大小是固定的(同样,T 的 10 元素数组与 T 的 20 元素数组是不同的类型)。
To expand a little bit on some of the answers here...
In C, when an array identifier appears in a context other than as an operand to either & or sizeof, the type of the identifier is implicitly converted from "N-element array of T" to "pointer to T", and its value is implicitly set to the address of the first element in the array (which is the same as the address of the array itself). That's why when you just pass the array identifier as an argument to a function, the function receives a pointer to the base type, rather than an array. Since you can't tell how big an array is just by looking at the pointer to the first element, you have to pass the size in as a separate parameter.
There are a couple of alternate ways of passing arrays to functions.
There is such a thing as a pointer to an array of T, as opposed to a pointer to T. You would declare such a pointer as
In this case, p is a pointer to an N-element array of T (as opposed to T *p[N], where p is an N-element array of pointer to T). So you could pass a pointer to the array as opposed to a pointer to the first element:
The disadvantage of this method is that the array size is fixed, since a pointer to a 10-element array of T is a different type from a pointer to a 20-element array of T.
A third method is to wrap the array in a struct:
The advantage of this method is that you aren't mucking around with pointers. The disadvantage is that the array size is fixed (again, a 10-element array of T is a different type from a 20-element array of T).
C 语言不支持任何类型的引用传递。 最接近的等效方法是传递一个指向该类型的指针。
这是两种语言的人为示例
C++ 风格的 API
最接近的 C 等效项
The C language does not support pass by reference of any type. The closest equivalent is to pass a pointer to the type.
Here is a contrived example in both languages
C++ style API
Closest C equivalent