Hi,

I just wanted to clear something up regarding structs.

Why is it that I can declare a strut like this:

Code:
typedef struct date_header {
	uint16_t year;
	uint8_t month;
	uint8_t day;

} DATE;
And do this kind of thing without any problems:

Code Segement 1:

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);
However, if I do this:


Code Segment 2:


Code:
 DATE *datePtr; 

datePtr->year = 1;
datePtr->month = 2;
datePtr->day = 3;

printf("Year is: %d", datePtr->year);
It gives me a segmentation fault at the point where year is being assigned a value.

For some reason, doing this works:

Code Segment 3:
Code:
DATE date, *datePtr; 

datePtr = &date;

datePtr->year = 1;
datePtr->month = 2;
datePtr->day = 3;

printf("Year is: %d", datePtr->year);
How come I don't need to declare:
Code:
DATE date, *datePtr; 

datePtr = &date;
for the code in segment 1?

Thanks.