Declare member of struct as a pointer in c

I have a uint8_t C = malloc(24sizeof(uint8_t));

I need to send some integers and this *C to another node(in ad hoc network). So I am going to use a struct

` struct fulMsg { 
int msgType; 
int msgCount; 
//uint8_t *CC;
} fulMsg_t;

typedef struct fulMsg fulMsg_tt;`

there is a method called

 packetbuf_copyfrom(X, sizeof(X));

in my api(contiki). If I create struct like this

struct fulMsg *fmsg = &fulMsg_t;

I can use the above method like this

packetbuf_copyfrom(fmsg, 8);

and from the other end I can easily get those two values.

So my problem is when I am going to apply same thing to that pointer it is not working the network simulator that I am using suddenly be crashed (I think there is a segmentation fault). I can't initialize size of *C in the struct no. And how correctly do this fmsg->CC = C;

At the other end this is how I receive this struct is

struct fulMsg *r_fmsg = &fulMsg_t;

and

rfmsg = (fulMsg_tt *)(packetbuf_dataptr());

. So I can easily get values from other end. (No need to do ntoh and hton, but it is ok if this really needs that)

In simple what I want to do is send the value of *C contain and some other integers to another node. How can I correctly do this.

related question post by me regarding this project http://www.unix.com/programming/201223-segmentation-fault-when-debugging-c.html
Thanks

When you send a pointer you are NOT sending data. You are sending an address.

typedef 
 struct fulMsg { 
int msgType; 
int msgCount; 
uint8_t  *CC;
} fulMsg_t;

You are passing a reference, not the string of characters or binary data array you are pointing to. The reference on the other node is pointing to nothing. That is why you segfault. Unless your method is smart enough to create storage on the remote side and copy the array over there.

1 Like