I wonder about the memory area of the pointer!

Asked 2 years ago, Updated 2 years ago, 42 views

void swap(int *a, int *b){
 int temp=*a; 
 *a=*b;
 *b=temp;
}

As an example of call by reference in this way, we use pointers in frequently used functions.

While studying the memory area, I found out that the pointer was stored in the stack area If so, all variables will be erased after the method comes out, so how can the value change in the main be checked?

Maybe I don't understand it because I don't have enough understanding of the memory area TT...

Is the address value used in the pointer separate from the memory area?

c pointer

2022-09-21 18:49

1 Answers

Pointers are not stored in the stack, but parameters or regional variables are stored in the stack.

Of course, in addition to the variables, the return address is also saved by the call opcode (this is independent of the above source).)

Variable cleared (stack cleared to be exact)...The reason why this function is valid is that the pointer variables a and b, which are parameters, are addressed to the regional variables created in the main.

This means that the address of the variable created in main will be entered as a parameter of the swap function, so it will not be removed from the swap function.

You need to be familiar with the system structure because the c language was originally designed as the system language.


2022-09-21 18:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.