krikara
krikara

Reputation: 2435

How to access members for struct in struct - C

struct sniff_ip {
struct in_addr ip_src, ip_dst;
}

function{
const struct sniff_ip *ip;
ip = (struct sniff_ip*)(packet +SIZE_ETHERNET);

}

My goal here is to change the values of ip_src and ip_dst, but I can't figure out the correct syntax to modify src or dst when it is in a struct within a struct. I know to access a member within a struct is normally ip->member or (*ip).member , but this doesn't seem to be working for me here. What are the correct procedure(s) for accessing members in the event they are in a struct within a struct?

Edit: I want to change the ip addresses (values) for both src and dst. When using lines such as

"ip->ip_src="
or "ip->ip_src.s_addr=" , I get the error that "assignment of read-only location '*ip'

Upvotes: 2

Views: 2177

Answers (3)

Eregrith
Eregrith

Reputation: 4367

You problem is:

const struct sniff_ip *ip;

instead of:

struct sniff_ip const *ip = ...;

In your current declaration, you have ip, a pointer to a const data of type struct sniff_ip.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

You simply combine the operator in the correct places:

ip->ip_src.s_addr

Upvotes: 3

Borealid
Borealid

Reputation: 98459

struct in_addr other_addr;
ip->ip_src = other_addr;

Upvotes: 0

Related Questions