C lang basic issue - why did I get the same addresses to two struct? -


while (1)  {     struct entry n =     {         ele[i], null, 1     };     printf("%d", &n); // todo same address } 

this program keeps printing same address, isn't struct entry n = xxx operation "new" in c++ , java? much.

no, it's not @ same. here creating struct allocated space compile-time , have same address time. there no need create new 1 ever. setting values inside struct.

the value allocated in stack , since in same stack frame, stay same.

if change code calling function, value change. example

void test1() {     struct entry n =     {         ele[i], null, 1     };     printf("%d", &n); }  void test2() {     test1(); }  void main() {     test1();     test1();     test2();     test1();     test2(); } 

from first 2 (most likely) same address, since there same stack modifications in calls. when call test2(), calls test1() address different, since stack frame different.

but in case address of struct must not stored anywhere, since temporary local variable, disappear after returning function , dereferencing invoke undefined behaviour.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -