'Ptr' is used uninitialized in this function
I do not understand if I declare the pointer as a structure variable, why is become uninitialized ?
program code
Code:
#include <stdio.h>
#include <stdlib.h>
struct person
{
char code;
int age;
};
int main()
{
struct person *Ptr;
Ptr->age = 30;
Ptr->code='X';
printf("Age: %d\n", Ptr->age);
printf("code: %c", Ptr->code);
return 0;
}
complier give warning
'Ptr' is used uninitialized in this function
if I allocate dynamic memory for pointer variable then code run fine
Code:
struct person *Ptr = malloc (sizeof(Ptr));
give output
Age: 30
code: X
What is the problem with the previous code line ? why compiler doesn't allocate memory for pointer ? Is it necessary to always allocate dynamic memory if variable is pointer