C language: a typical case that involves the pointer function return value and printf garbled and memory stack. A typical case of printf
A strange C language problem involves pointers, arrays, stacks, and printf. The following implementation: converts Integers to strings, returns string pointers, and CALLS printf in the main function for display.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* switch(int n)
{
char A[20],B[20];
char*p;//=(char*)malloc(4*sizeof(char));
int i=0,a;
int minus=0;
if(n<0)
{
minus=1;
n=-n;
}
while(n/10!=0)
{
a=n%10;
n=n/10;
A[i++]='0'+a;
}
a=n%10;
A[i++]='0'+a;
if(minus==1)
A[i++]='-';
A[i]=0;
int len=i;
int j=len-1;
i=0;
while(i<len)
{
B[i]=A[len-1-i];
i++;
}
B[i]=0;
p=B;
printf("%s,",p);
return p;
}
void main()
{
int a=-234;
char* p=swich(a);
printf("%s,",p);
}
The execution result of the preceding program is as follows: In the swich function, 234 can be output normally. The output in main is garbled.
for(int i=0;i<3;i++)
{
printf("%c",p[i]);
}
Then only '2' can be correctly output, p [1], p [2] garbled.
Why? Before calling the function printf, you must press the form parameter on the stack. * p is calculated at this time. Therefore, the first printf statement has calculated the parameters and saved them on the top of the stack. Then call the printf function (the function call needs to use the stack to establish an access connection and control chain. The original function f is executed, and the original function f is at the top of the stack. Therefore, the stack space of function f is released. Array space is also released), printf occupies the stack, so the original function f stack space content is modified. Therefore, the first printf statement can obtain the result. Since the content of the arr space has been modified, subsequent printf statements cannot get the result.
By the way, I will explain why printf ("% s \ n", p); is garbled.
As mentioned above, calculate the value of parameter p to save the stack top. The saved value is the address of arr. Then call the printf function to modify the content of the top stack space. Although the address is saved, the original content has been modified, so no result is obtained.
Source: http://zhidao.baidu.com/question/326625441.html>
A strange C language problem involves pointers, arrays, stacks, and printf.
I didn't want to understand it just now. I saw the answer from dripple11 and thought about it again. Now I want to understand it. His answer is correct.
I looked back and found that lz did not fully understand it.
By the way, whatplay's answer is silly.
Printf function, as the first person said, before calling the printf function, you must first press the form parameter on the stack. At this time, you need to calculate * p
Therefore, the first printf statement has calculated the parameters and saved them on the top of the stack. Then call the printf function (the function call needs to use the stack to establish an access connection and control chain. However, the original function f is executed, and the original function f is at the top of the stack. Therefore, the stack space of function f is released. Array space is also released), printf occupies the stack, so the original function f stack space content is modified. Therefore, the first printf statement can obtain the result. Since the content of the arr space has been modified, subsequent printf statements cannot get the result.
By the way, I will explain why printf ("% s \ n", p); is garbled.
As mentioned above, calculate the value of parameter p to save the stack top. The saved value is the address of arr. Then call the printf function to modify the content of the top stack space. Although the address is saved, the original content has been modified, so no result is obtained.
Whataplay, I just despise your answer and didn't scold you. Because your answer is not correct!
In C language, the return value of a pointer sub-function is the address or the memory content in the address.
First, let's take a look at the meaning of int * a (int * B:
Defines a function whose return value type is Integer pointer (int *). The function name is a. This function needs to pass a parameter B. the type of this parameter is also integer pointer (int *).
Therefore, an integer pointer should be input to the place where the function is called.
Here we emphasize the three points of pointer: ① pointer is a special variable; ② the value stored in the pointer is a memory address; ③ several bytes starting from the memory address with the value of the pointer variable store a value of this type.