C language: a typical case that involves the pointer function return value and printf garbled and memory stack. A typical case of printf

Source: Internet
Author: User

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.

 
 
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. char* switch(int n)
  5. {
  6. char A[20],B[20];
  7. char*p;//=(char*)malloc(4*sizeof(char));
  8. int i=0,a;
  9. int minus=0;
  10. if(n<0)
  11. {
  12. minus=1;
  13. n=-n;
  14. }
  15. while(n/10!=0)
  16. {
  17. a=n%10;
  18. n=n/10;
  19. A[i++]='0'+a;
  20. }
  21. a=n%10;
  22. A[i++]='0'+a;
  23. if(minus==1)
  24. A[i++]='-';
  25. A[i]=0;
  26. int len=i;
  27. int j=len-1;
  28. i=0;
  29. while(i<len)
  30. {
  31. B[i]=A[len-1-i];
  32. i++;
  33. }
  34. B[i]=0;
  35. p=B;
  36. printf("%s,",p);
  37. return p;
  38. }
  39. void main()
  40. {
  41. int a=-234;
  42. char* p=swich(a);
  43. printf("%s,",p);
  44. }
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.
 
 
  1. for(int i=0;i<3;i++)
  2. {
  3. printf("%c",p[i]);
  4. }
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.