Rory Flast
Rory Flast

Reputation: 83

C- Passing and Deferencing Structure Arrays

I have this C code for a Point structure:

typedef struct Point{
 int x, y;
} Point;

void one(Point P) {
 p.x = 1;
 p.y = 1;
}

I would like to make a pointer to an array of 50 points and then pass these some of the array members to the one function.

I tried

struct Point (*mypointer)[50];
one(mypointer[i]);

but got "expected 'Point' but argument is of type 'struct Point'" I then tried deferencing the pointer

one(&mypointer[i])

but got "expected 'Point' but argument is of type 'struct Point (*)[(long unsigned int)N]" What should I do? Thanks.

Upvotes: 0

Views: 74

Answers (1)

Jack
Jack

Reputation: 133567

That's because argument for function one is not a reference to a structure but just real data (which is allocated on the stack).

Try by using:

void one(Point *p) {
 p->x = 1;
 p->y = 1;
}

In any case the array declared as struct Point *mypointer[50] is not an array to points but an array of pointers to points (which you have to allocate one by one). If you wish to have just an array to points you should use

Point points[50];
one(&points[i])

Upvotes: 2

Related Questions