user3291818
user3291818

Reputation: 203

Invalid argument type

I'm trying to compile my code but I keep getting this error.

error: invalid type argument of '->' (have 'struct packet')

Parts of the initialization:

struct list *current;
struct packet p;

struct packet *newpacket = malloc(sizeof(p));

This line is causing the error, but I don't know what's wrong.

if ((current -> p -> block_num < newpacket -> block_num) && (current -> next -> p -> block_num > newpacket -> block_num)){

My header files:

struct packet {
unsigned short  block_num;
unsigned short  block_size;
unsigned short  crc;
unsigned char  *payload;
};

struct list {
struct packet p;
struct list *next;
};

I don't understand what's wrong, and I'm really new to C. Any help would be appreciated. Thanks!

Upvotes: 1

Views: 1799

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114320

The error is caused by incorrectly dereferencing of the p element in current -> next -> p -> block_num and current -> p -> block_num.

They should be current -> p.block_num and current -> p.block_num, respectively.

Notice that you declare p in struct list as struct packet p; not as struct packet *p;.

That means that the two references in the left side of each comparison are actual structs, not pointers to structs.

Upvotes: 0

bblincoe
bblincoe

Reputation: 2483

The -> operator is used in instances where you have a pointer to an object and want to access the objects members. In this case, you have an instance to a packet structure which is not a pointer. You are trying to access its members with the -> operator. This is incorrect. Instead you should use the . operator.

For example: current->p.block_num would give you the packet block number.

The following is described in this Wikipedia article:

  • a->b is known as a structure dereference (member b of object pointed to by a).
  • a.b is known as a structure reference (member b of object a).

Upvotes: 1

Related Questions