0

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.

8
  • 4
    host[i]->nnic : host[i] is not pointer. change to host[i].nnic Commented Oct 15, 2014 at 19:48
  • but it is pointer: host_t *host Commented Oct 15, 2014 at 19:50
  • 3
    No. host is a pointer to host_t, therefore host[i] is a host_t. Commented Oct 15, 2014 at 19:52
  • 1
    when Type *pointer, pointer[i] meant *(pointer + i). type of pointer dereference is Type. Commented Oct 15, 2014 at 19:56
  • 1
    it is not obvious, but I'll know Commented Oct 15, 2014 at 20:00

5 Answers 5

2

You are confusing the -> operator with the . operator. If you have a struct st, then you can access its members by st.member. If you have a struct pointer st_ptr, then st_ptr->member is short for (*st_ptr).member. In your case, you are dereferencing the pointer with the array index.

Sign up to request clarification or add additional context in comments.

Comments

1

You want to use . and not ->.

host[i]->nnic = 3;

should be

host[i].nnic = 3;

Comments

1

You have an array of host_t structs, not host_t * pointers. So you want to say, e.g.:

host[i].nnic = 3;

Comments

1

You already have an array of structures. host[i] is a structure, not a pointer. So . instead of -> is what you need.

Comments

1

host[i] is not a pointer, it represents a single instance of host_t,

therefore you must use the . operator, not the -> operator.

Change: (and all other occurrences)

From:  

host[i]->nnic = 3;  

To:  

host[i].nnic = 3;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.