简体   繁体   English

是否可以将指针和数组作为函数参数传递?

[英]Is it possible to pass a pointer to and array as a function parameter?

I have two integer arrays created at runtime (size depends on the program input). 我在运行时创建了两个整数数组(大小取决于程序输入)。 At some point I need to update the contents of an array with the contents of the other doing some calculations. 在某些时候,我需要使用其他数组的内容来更新数组的内容,以进行一些计算。

First I thought about passing those arrays as parameters to a function because I didn't find a way to return functions in C (don't think it's possible). 首先,我考虑将这些数组作为参数传递给函数,因为我没有找到在C中返回函数的方法(认为不可能)。 After realizing that was a bad idea since parameters are not really modifiable as they're copied to the stack I resorted to change to array pointers instead. 意识到这是一个坏主意之后,因为参数实际上无法修改,因为它们已被复制到堆栈中,所以我改为改为使用数组指针。

While the function is still empty, this is the code I have: 当函数仍然为空时,这是我的代码:

1st take (code compiles, no errors): 第一步(代码编译,没有错误):

// Elements is just to be able to iterate through their contents (same for both):
void do_stuff(int first[], int second[], int elements) {}

// Call to the function:
do_stuff(first, second, elements);

2nd take, attempt to translate to pointers to be able to modify the arrays in place: 第二步,尝试转换为指针以能够在适当的位置修改数组:

void do_stuff(int *first[], int *second[], int elements) {}

// Call to the function:
do_stuff(&first, &second, elements);

This code lead to some rightful compile time errors, because apparently what I thought to be pointer to arrays were arrays of pointers. 这段代码会导致一些正确的编译时错误,因为显然我认为指向数组的指针是指针的数组。

3rd take, what I think it'd be the right syntax: 第三点,我认为这是正确的语法:

void do_stuff(int (*first)[], int (*second)[], int elements) {}

// Call to the function:
do_stuff(&first, &second, elements);

Still this code produces compile time errors when trying to access the elements of the arrays (eg *first[0] ): 在尝试访问数组的元素时,此代码仍然会产生编译时错误(例如*first[0] ):

error: invalid use of array with unspecified bounds

So my question is regarding the possibility of using an array pointer as a parameter of a function, is it possible? 所以我的问题是关于使用数组指针作为函数参数的可能性,这可能吗? If so, how could it be done? 如果是这样,怎么办?

Anyway, if you think of a better way to update the first array after performing calculations involving the contents of the second please comment about it. 无论如何,如果您在执行涉及第二个数组内容的计算后想出一种更好的方式来更新第一个数组,请对此发表评论。

An array decays to a pointer to the data allocated for the array. 数组衰减为指向为该数组分配的数据的指针。 Arrays are not copied to the stack when passing to functions. 传递给函数时,不会将数组复制到堆栈中。 Thus, you needn't pass a pointer to the array. 因此,您无需将指针传递给数组。 So, the below should function fine. 因此,以下功能应该可以正常运行。

 // Elements is just to be able to iterate through their contents (same for both): void do_stuff(int first[], int second[], int elements) {} // Call to the function: do_stuff(first, second, elements); 

The cause of your errors on your second attempt are because int *first[] (and the others like it) are actually of the type array of pointer to int . 您第二次尝试出错的原因是因为int *first[] (以及其他类似的东西)实际上是指向int的指针类型数组

The cause of your third errors are because *first[N] is actually *(first[N]) , which cannot be done with ease. 第三个错误的原因是因为*first[N]实际上是*(first[N]) ,这很难做到。 Array access is really a facade over pointer arithmetic, *(first + sizeof first[0] * N) ; 数组访问实际上是指针算法*(first + sizeof first[0] * N) however, you have an incomplete element type here -- you need to specify the size of the array, otherwise sizeof first[0] is unknown. 但是,这里的元素类型不完整-您需要指定数组的大小,否则sizeof first[0]是未知的。

Your first attempt is correct. 您的第一次尝试是正确的。 When passing an array as a parameter in C, a pointer to the first element is actually passed, not a copy of the array. 在C中将数组作为参数传递时,实际上会传递指向第一个元素的指针,而不是数组的副本。 So you can write either 所以你可以写

void do_stuff(int first[], int second[], int elements) {}

like you had, or 像你以前那样,或者

void do_stuff(int *first, int *second, int elements) {}

In C arrays automatically decay to pointers to data, So, you can just pass the arrays and their lengths and get the desired result. 在C数组中,数据会自动衰减为指向数据的指针,因此,您只需传递数组及其长度即可获得所需的结果。

My suggestion is something like this: 我的建议是这样的:

void dostuff(int *first, int firstlen, int *second, int secondlen, int elements)

Function call should be: 函数调用应为:

 do_stuff(first, firstlen, second, secondlen, elements);

I am not very clear from your question, why you need elements . 从您的问题中我不清楚,您为什么需要elements But, you must pass array lengths as arrays automatically decays to pointers when passed to a function, but, in the called function, there is no way to determine their size. 但是,您必须传递数组长度,因为数组在传递给函数时会自动衰减为指针,但是在调用的函数中,无法确定其大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM