简体   繁体   中英

passing array of structure pointer to a function

I am writing a program in which I have to pass an array of structure pointers to a function in main body as follows

     struct node *vertices[20];
create_vertices (&vertices,20);

implementation of function is some thing like this

void create_vertices (struct node *vertices[20],int index)
{
}

in this I have to pass an array of structure pointers with index 20, the declaration I did outside mains is as follows I

void create_vertices(struct node **,int);

However each time compiling the code gives me problem in these three lines only as

bfs.c:26:6: error: conflicting types for ‘create_vertices’
bfs.c:8:6: note: previous declaration of ‘create_vertices’ was here
bfs.c: In function ‘create_vertices’:
bfs.c:36:15: error: incompatible types when assigning to type ‘struct node’ from type ‘struct node *’

I am unable to understand how should I be doing this. What I want to be able to do is:

  1. Declare an array of structure pointers in main (which I already did).
  2. Pass the address of array to function (here is where I goofed up).
  3. Declare the correct prototype of function outside mains.

The code has to be on C and I am testing it on Linux. Can some one point me?

The type of &vertices in the call create_vertices(&vertices, 20) is not what you think.

It is a pointer to an array of pointers to structs:

struct node *(*)[20]

and not

struct node **

Drop the & in the call and you'd be back in business.

The compilation (using GCC 4.7.0 on Mac OS X 10.7.4):

$ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -c x3.c
x3.c: In function ‘func1’:
x3.c:16:9: warning: passing argument 1 of ‘create_vertices’ from incompatible pointer type [enabled by default]
x3.c:7:10: note: expected ‘struct node **’ but argument is of type ‘struct node * (*)[20]’
$

The code:

struct node { void *data; void *next; };

void make_node(struct node *item);
void func1(void);
void create_vertices(struct node **array, int arrsize);

void create_vertices(struct node *vertices[20], int index)
{
    for (int i = 0; i < index; i++)
        make_node(vertices[i]);
}

void func1(void)
{
    struct node *vertices[20];
    create_vertices(&vertices, 20);
}

Drop the & and the code compiles cleanly.

As you wrote: struct node *vertices[20]; declares an array of pointers to node. Now if you want to create a function that changes its elements, you should declare a function that takes this kind of array as an argument:

void create_vertices(struct node *arr[20], int size)

or since the size can be ommited in this case, it's better to declare it as:

void create_vertices(struct node *arr[], int size)

Note, that this function can be called like this: create_vertices(vertices, 20); which makes first argument of this function ( arr ) to point to first element of this array. You are able to change this array within this function and changes will be visible outside.

Let's say you have function void foo(struct node *ptr) that changes node that ptr points to. When you declare struct node *ptr; and pass to this function: foo(ptr); , it can change this node object and changes are visible outside, but it can't change the passed pointer ptr itself. When you need to change pointer within the function so that changes are visible outside, that's the situation when you pass an address of pointer to function taking pointer to pointer.

In the prototype of create_vertices , the first argument is a pointer to a pointer to a structure. In the definition the first argument is array of 20 pointers to a structure.

Both the prototype and the definition has to be the same.

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