-
pointer to a struct
i have this struct:
struct temp_rec
{
int iYear;
int iMonth;
int iTemp;
int iLoc;
char* sLoc;
}*trPtr;
and im trying to write values into the pointer and read from the pointer like this:
printf("Please enter the location: ");
scanf(trPtr->sLoc);
its causing a stack dump so i know there is something wrong. There is nothing that goes near the struct anywhere else in the program. what am i doing wrong? I want to use pointers to conserve memory usage. plus i havent really used pointers much, i could do with the practice.
-
You have to have the pointer aimed at memory where the datatype lives. Here is one way to do that:
Code:
struct temp_rec
{
int iYear;
int iMonth;
int iTemp;
int iLoc;
char* sLoc;
}*trPtr, mystruct;
trPtr = &mystruct;
-
And you have to allocate memory for the string.