Hi,
I just wanted to clear something up regarding structs.
Why is it that I can declare a strut like this:
And do this kind of thing without any problems:Code:
typedef struct date_header {
uint16_t year;
uint8_t month;
uint8_t day;
} DATE;
Code Segement 1:
However, if I do this:Code:DATE *datePtr;
uint8_t rx_buffer[4];
// assume the rx_buffer has been filled with 4 bytes of data at this point
datePtr = (DATE *)rx_buffer;
printf("Year is: %d", datePtr->year);
Code Segment 2:
It gives me a segmentation fault at the point where year is being assigned a value.Code:DATE *datePtr;
datePtr->year = 1;
datePtr->month = 2;
datePtr->day = 3;
printf("Year is: %d", datePtr->year);
For some reason, doing this works:
Code Segment 3:
How come I don't need to declare:Code:DATE date, *datePtr;
datePtr = &date;
datePtr->year = 1;
datePtr->month = 2;
datePtr->day = 3;
printf("Year is: %d", datePtr->year);
for the code in segment 1?Code:DATE date, *datePtr;
datePtr = &date;
Thanks.