i have code, but it doesn't compile
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned int ip;
unsigned int mask;
} nic_t;
typedef struct {
int nnic; /*number of nic*/
nic_t *nic;
} host_t;
int main(void)
{
int i, j;
host_t *host = malloc(sizeof(host_t) * 5);
for (i = 0; i < 5; i++) {
host[i]->nnic = 3;
host[i]->nic = malloc(sizeof(nic_t) * 3);
for (j = 0; j < host[i]->nnic; j++) {
host[i]->nic[j]->ip = 0x7f000001;
host[i]->nic[j]->mask = 0xffffff00;
}
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
printf("ip = 0x%x, mask = 0x%x\n",
host[i]->nic[j]->ip,
host[i]->nic[j]->mask);
}
printf("\n\n");
}
return 0;
}
please explain to me how I need to address an array of structures in the program, that there was no compilation errors. I have the following errors:
test.c: In function ‘main’:
test.c:18:10: error: invalid type argument of ‘->’ (have ‘host_t’)
test.c:19:10: error: invalid type argument of ‘->’ (have ‘host_t’)
test.c:21:11: error: invalid type argument of ‘->’ (have ‘host_t’)
test.c:22:11: error: invalid type argument of ‘->’ (have ‘host_t’)
test.c:28:13: error: invalid type argument of ‘->’ (have ‘host_t’)
test.c:29:13: error: invalid type argument of ‘->’ (have ‘host_t’)
thanks for all. it works.
host[i]->nnic:host[i]is not pointer. change tohost[i].nnichostis a pointer tohost_t, thereforehost[i]is ahost_t.