Originally Posted by
Hodor
It's not always necessary to allocate dynamic memory but the pointer has to point to something valid before you use it (called dereferencing). E.g..
big thanks I have query I think there is only three variables in code Ptr, Ptr->age and Ptr->code
What is person ? Is it a variable that occupy memory ?
Code:
#include <stdio.h>
#include <stdlib.h>
struct person
{
char code;
int age;
};
int main()
{
struct person person;
struct person *Ptr = &person;
Ptr->age = 30;
Ptr->code='X';
printf("address of person : %p \n", &person);
printf("address of Ptr : %p \n", &Ptr);
printf("address of Ptr->age = %p \n", & Ptr->age);
printf("address of Ptr->code = %p \n", &Ptr->code);
printf("Age: %d\n", Ptr->age);
printf("code: %c", Ptr->code);
return 0;
}
address of person : 0061FF28
address of Ptr : 0061FF24
address of Ptr->age = 0061FF2C
address of Ptr->code = 0061FF28
Age: 30
code: X