简体   繁体   中英

Passing an array of pointers to a struct to a function

I have a function where an array of pointers to a struct is declared

void functionA(argument1, argument2) {
   ...
   struct1 *point[2];
   ...

   functionB(*point, ...) // Call the function ?
}

// Declare the function ?
void functionB(struct1 *point[], ...) {


}

I need to pass this array *point[2] to another function say functionB where i need to so some operations based on the point[0] and point[1] what is the correct way to do this ?

When I am calling the functionB in functionA like functionB(*point, ...) , I am getting the error of incompatible pointer types whereas when I am calling it like functionB(*point[], ...) I am getting the error that expected expression before ] token .

Your function call is wrong. functionB is expecting its argument of type struct1 ** but you are passing an argument of type struct1 * . Function call should be

functionB(point, ...);  

You should know that as per C rule, when an array is passed to a function then array decays to pointer to it's first element. point in function call will decay and will have type struct1 ** after decaying.

Here is one way of doing it:

void functionA(argument1, argument2) {
   ...
   struct1 *point[2];
   ...

   functionB(point, ...) // Call the function ?
}

// Declare the function ?
void functionB(struct1 ** point, ...) {

  // Use point[1] and point[2] here
}

When you need to send an array to a function, you just have to send the base address of that array (ie the name) to that function, and then receive it using a pointer to that type. And this rule applies to every data type, be it an int or a struct .


So, You can do

void functionA(argument1, argument2) {
   ...
   struct1 *point[2];
   ...

   functionB(point, ...) // Sending base address of array point
}

// Recieve it this way
void functionB(struct1 *point[], ...) {

// Or
void functionB(struct1 **point, ...) {

Function functionB is declared as having the first parameter of type struct1 *point[]

void functionB(struct1 *point[], ...) {


}

And the array point has the same type of elements.

So you just need to use the name of the array as the argument for the function call

functionB(point, ...) // Call the function ?

Take into account that parameters declared as arrays are adjucted to pointers to their elements.

Thus this function declarations

void functionB(struct1 *point[], ...);

and

void functionB(struct1 **point, ...);

are equivalent and declare the same one function.

On the other hand array designators used in expressions are converted to pointers to their first elements. So the expression point used as an argument is converted to pointer of type struct1 ** that is it exactly corresponds to the expected by the function argument according to its declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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