Start New Discussion within our Software Development Community

Hi all,

I am currently extending a large C project with an own extension and I am stuck in a pointer/array/struct problem:

The project offers a struct of this structure

struct ip_addr{
	unsigned int af; /* address family: AF_INET6 or AF_INET */
	unsigned int len;    /* address len, 16 or 4 */
	
	/* 64 bits aligned address */
	union {
		unsigned long  addrl[16/sizeof(long)]; /* long format*/
		unsigned int   addr32[4];
		unsigned short addr16[8];
		unsigned char  addr[16];
	}u;
};

I can access the addr32 of a messages-IP-address by calling msg->rcv.src_ip.u.addr32 Now, I want to copy the content of the this ip_addr-struct into a struct into another struct stored in an array:

typedef struct downstream {
	struct ip_addr* addr;	
	unsigned short src_port;
	short 	proto;	
	int	droprate;   //droprate, communicated with oc=xxx
	time_t	expires_ts; //timestamp
	int	oc_validity;//duration
	double	oc_seq;     //sequence
} downstream_t;

...

downstream_t* downstream[ARRAYSIZE];

I tried it with downstream[1]->addr=&(msg->rcv.src_ip); but if I understood C-Pointers correctly, I just create a reference to the Memory Area where the msg->rcv.src_ip is stored.

My next try was to memcpy the content of the rcv.src_ip to downstream[1]->addr by something like memcpy((struct ip_addr)downstream[ndownstream_mp]->addr,(struct ip_addr)msg->rcv.src_ip,sizeof(struct ip_addr)); , but this failed too.

Any help is appreciated
Marc

> if I understood C-Pointers correctly, I just create a reference to the Memory Area where the msg->rcv.src_ip is stored

Your understanding is correct. However, an addr field may only store a reference. If the msg is transient, you need to create a permanent copy of src_ip, for example,

downstream[1]->addr = malloc(sizeof(struct ip_addr));
    *downstream[1]->addr = msg->rcv.src_ip;
This article has been dead for over six months. Start a new discussion instead.