Let's look at the following topics to see if you can give an answer and really understand why.
#include "stdio.h" #include "string.h" #include "malloc.h" void swap (int a,int b) {int temp;temp = A;a = B;b = temp;} int get_int (int a) {int i = 1 + a;return i;} char* Get_memory0 () {Char *p = (char *) malloc (sizeof (char) *); strcpy (P, "Hello World"); return p;} char* Get_memory1 () {char *p = "Hello World"; return p;} char* Get_memory2 () {char p[] = "Hello World"; return p;} int main () {int X=4,y=3;swap (x, y), int z=x-y;printf ("z =%d\n", z);//Problem 1z=get_int (z);p rintf ("z =%d\n", z);//problem 2char* C0 = Get_memory0 ();p rintf ("C0 =%s\n", c0);//problem 3const char* C1 = get_memory1 ();p rintf ("C1 =%s\n", C1);//problem 4char* Const C2 = get_ Memory2 ();p rintf ("C2 =%s\n", c2);//Issue 5}
Answer to question 1: z=1, for this answer, I believe many people will not answer wrong, all know that A and B did not happen to exchange. But do you understand the reason?
Swap (int a,int b) is a parameter-worthy function, which means that A and B in the function body are a partial copy of the arguments int A and B in the function, so a and B can actually be considered as local variables, and their values are copied from the arguments of int A and int b. The values of a and B within the function are only valid within the function body, and A and B variables are destroyed when the A and B variables in the function body leave the function scope. The actual parameter values of the functions have not changed, or have not changed from start to finish, but only a copy of their local copy a and B in the function body.
Answer to question 2: z=2
int get_int (int a) is a function of the value of the return value, which means that the function int get_int (int a) produces a temporary object at the return of the function, where the user holds a copy of the value of the local variable i (a copy of the right value of the variable i), the temporary object is not a name, The value (right value) of the object without a name is stored in the caller's stack. So when I left the scope as a local variable, it was destroyed, but its copy still existed and was assigned "Lvalue Z" as "rvalue" when the function returned.
Answer to question 3: c0= "Hello World"
The function returns the pointer to "heap" memory. malloc () is a library function for allocating "heap" memory, and for "heap" memory, the "heap" memory allocated by the malloc () library function will persist during program run as long as the free () library function is not called in the program to release the heap memory. char* Get_memory0 () produces a copy of the "left value" of the team return variable p at the return of the function, which means that the address to the heap is stored in the copy of the "left value". P, as a local variable, is destroyed when it leaves the scope of the function, but the copy of the "Lvalue" returned by the function is present, and the copy stores the address that points to the heap, and the value of the heap is "Hello World".
Answer to question 4: c1= "Hello World"
In char* p = "Hello World", the left value is the local pointer variable p, stored on the function stack, and the right value is the string constant "Hello World", stored in the constant store.
char* get_memory 1 () is a function that returns a pointer, which means that the function char* get_memory 1 () produces a copy of the "left value" of the returned object at the return of the function, that is, the "lvalue" copy that stores the pointer to the string constant "Hello World "address. P, as a local variable, is destroyed when it leaves the scope of the function, but the "Lvalue" copy returned by the function still stores the address of the string constant "Hello World" pointing to the constant store.
Answer to question 5: c2= unknown
The return value is a pointer that produces an "lvalue" copy when the function returns.
The "left value" copy returned by the function points to the first address of the local variable array p[12]. The local array p[12] is automatically destroyed when it leaves the scope. At this point, the "Lvalue" copy returned by the function points to the address of a local variable that was destroyed.
c#-access bomber, new process, end process ... (ConsoleApp)---Shinepans