typedef struct {
double x;
double y;
} point;
point p1;
point* p2 = malloc(sizeof(point));
In this code where the variables p1,p2,p1.x, and p2->x are stored in stack or heap memory?
Where the mentioned variables are stored in stack or heap memory?
p1has static storage duration (lifetime same as the whole program)p2has automatic storage duration (lifetime same as the functionfoo)*p2(object referenced by the pointerp2) has allocated storage duration (lifetime same as the whole program or until it isfreeed)C standard (language) does not say where those objects are stored (it is up to implementation) and it does not matter from the programmer's point of view. Only the storage duration is important for you (because you need to know how long the object lives). Do not focus on implementation details when you are learning.
C language doesn't know anything about stack or heap but the most popular implementations place
.bssor.datasections (depending if they are initialized or not). Constant ones can be stored in.rodatasegment