-1

my code is as follows

    struct packetheader
    {
       __u16 fcf;
       __u8 seq;
       __u8 dest[16];
       __u8 src[16];
      #if defined dis   
       __u32 dispatch;
      #endif
    }
    struct packetheader* uncompressed()
    {
       struct packetheader *pkhdr;
       pkhdr->dispatch=0x00000000;//segmentation fault
       return pkhdr;
     }

getting a segmentation fault, when trying to assign values to dispatch which is __u32 type

user1707718
  • 21
  • 1
  • 3

1 Answers1

0

The problem is that pkhdr is uninitialized. pkhdr is a pointer and so by default it points to nothing. You need to either statitcally allocate an object and assign its address to pkhdr or dynamically allocate an object and assign it to pkhdr.

Example:

struct packetheader *pkhdr = malloc(sizeof(struct packetheader));
John Schug
  • 429
  • 1
  • 3
  • 8