I am rather new to C and although this code compiles (using gcc -Wall -o test test.c), running ./test just results in the output dbecher$: Segmentation fault: 11. Does anybody know what might cause this?
Well, I know instead of using a pointer I could just assign a normal struct, but I am using this to test some behavior of pointers. So I want to keep using a pointer variable for that struct.
Here the code snippet:
#include <stdio.h>
typedef struct HumidityMessage {
int nodeId;
int sequenceNumber;
int humidity;
} HumidityMessage;
HumidityMessage* packet;
int main() {
packet->nodeId = 0;
packet->sequenceNumber = 1;
packet->humidity = 3;
printf("This is the address of packet: %d", packet->nodeId);
}
packetis a null pointer. And then you are trying to dereference it.