First off, the C standard says nothing about stacks and heaps. Those are implementation details of a given compiler. That being said, most compilers for desktop applications use both of these.
You are correct that pa
is local to the main
function and therefore resides on the stack. *pa
however is not a local variable. It is an expression which evaluates to an instance of struct A
.
In this case, the malloc
function returns a pointer to a block of memory big enough for a struct A
and the value of that pointer is stored in pa
.
Generally speaking, anything returned by malloc
, realloc
, or calloc
lives in the heap, while variables declared local to a function (even pointer variables which may point to the heap) reside on the stack.