Hi,
I've been trying to allocate dynamicaly an array of pointers to structures. As it didn't work, I tried to reduce the problem as much as possible. This is what I have :
[code=c]
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
int y;
}struct_p;
int main()
{
int i,n=2;
struct_p *p;
p=calloc(n,size of(struct_p*));
for (i=0;i<n;i++)
{
p[i]->x=0;
p[i]->y=1;
}
}
[/code]
When compiling it with gcc, I get the errors:
new.c:18: error: invalid type argument of ‘->’
new.c:19: error: invalid type argument of ‘->’
I thought that by using calloc the way I did it, I had defined an array of pointers to structures. So the use of 'p[i]->x' for example, should give the content of the strucure field 'x' in the structure of adress 'p[i]'. Where am I wrong? What should I do?
Regards
I've been trying to allocate dynamicaly an array of pointers to structures. As it didn't work, I tried to reduce the problem as much as possible. This is what I have :
[code=c]
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
int y;
}struct_p;
int main()
{
int i,n=2;
struct_p *p;
p=calloc(n,size of(struct_p*));
for (i=0;i<n;i++)
{
p[i]->x=0;
p[i]->y=1;
}
}
[/code]
When compiling it with gcc, I get the errors:
new.c:18: error: invalid type argument of ‘->’
new.c:19: error: invalid type argument of ‘->’
I thought that by using calloc the way I did it, I had defined an array of pointers to structures. So the use of 'p[i]->x' for example, should give the content of the strucure field 'x' in the structure of adress 'p[i]'. Where am I wrong? What should I do?
Regards
Comment