Segmentation Fault by memcpy

Hello everybody,
I'm coding a test program for ARP protocol, and i don't know why i'm getting a SIGSEGV, i traced it with gdb and it says it's due to the memcpy function from /lib/libc.so.6.

Program received signal SIGSEGV, Segmentation fault.
0xb7e9e327 in memcpy () from /lib/libc.so.6

This segmentation fault came when i decided to change the protocol headers, i was using a buffer to store two structures, containing ethernet & arp headers, and i decided to use just one structure, containing both headers structure.

This is the code:

struct ethernet{
unsigned char target[6];
unsigned char source[6];
unsigned short type;
};

struct arp{
unsigned short ar$hrd;
unsigned short ar$pro;
unsigned char  ar$hln;
unsigned char  ar$pln;
unsigned short ar$op;
unsigned char  ar$sha[6];
unsigned char  ar$spa[4];
unsigned char  ar$tha[6];
unsigned char  ar$tpa[4];
};

struct arpmsg{
struct ethernet ethernet;
struct arp arp;
};

And i get the segmentation fault in the main program when i use memcpy to store the addresses in their respective field, here's an example:

char mac[]={0x01,0x00,0x5E,0x03,0x03,0x08};
memcpy(arpmsg->arp.ar$sha, mac, 6);

Please, could anyone tell me what am i doing wrong? This is a weird error, gcc gives me no warnings.

Oh!, i changed the memcpy function to strncpy, strcpy, and i keep getting the same SIGSEGV.

I'll appreciate any answers, thank you.

Your pointer arpmsg as in

arpmsg->arp.ar$sha

is NULL?

Hey, thanks for your reply,
No man, it is not a null pointer.

I know that data can't be copied to a NULL pointer, what else could the error be?

Run a debugger on the file to get the line in the ocde where the segfault occurred.
gcc example:

gcc -o myprog -g myprog.c
[run your code]
gdb myprog core
gdb> ba

the ba listing - the top one in your code (has a line number) - not in a library - is the point in your code where the problem was triggered.

From your post it looks like arpmsg is a struct tag not an instance of that type...and if you have a variable of that type calld aprmsg then you are using the wrong operator to access its members...

struct arpmsg
{
    struct ethernet ethernet;
    struct arp arp;
} arpmsg;

memcpy(arpmsg.arp.ar$sha, mac, 6);

jim_mcnamara, thank you very much for your reply, it has been helpful.

shamrock, you were right, you solved my problem, thank you so much for your help.