function return value array

Source: Internet
Author: User

First look at a subclass of the return value array: #include<stdio. h>
  1. #define N 5
  2. int *print()
  3. {
  4. int a[N];
  5. int i;
  6. for(i=0; I<n; I )
  7. A[i]=i;
  8. return a;
  9. }
  10. int main()
  11. {
  12. int *b, I;
  13. b=print();
  14. for(i=0; I<n; I )
  15. printf("%d\n", b[i]);
  16. return 0;
  17. }
This class is a function that returns an array, and the result is incorrect. The reason is that in the function print (), the array a[n] is a local variable, and when your function is finished, it will automatically release its space, so return a is simply returning a pointer to the array a[n] Value. In the main function, B should receive the address of the array a[n] (i.e. the address of the array itself), and the space occupied by it will be released with the completion of the function call, so the answer is incorrect. The return value of the function is an array, and the following two modifications will give the correct answer, the code is as follows:
  1. #include<stdio. h>
  2. #include<stdlib. h>
  3. #define N 5
  4. int *print()
  5. {
  6. static int a[N];
  7. int i;
  8. for(i=0; I<n; I )
  9. A[i]=i;
  10. return a;
  11. }
  12. int *print1()
  13. {
  14. int *a;
  15. int i;
  16. A=(int *) malloc(N);
  17. for(i=0; I<n; I )
  18. {
  19. A[i]=i;
  20. }
  21. return a;
  22. }
  23. int main()
  24. {
  25. int *b;
  26. // int b[N];
  27. int i;
  28. b=print1();
  29. for(i=0; I<n; I )
  30. printf("%d\n", b[i]);
  31. return 0;
  32. }
Whether you call the print () function or call the Print1 () function, you get the correct result. The reason for calling the print () function is to add the static keyword before the array a[n], which causes a[n] to be stored in the static storage area in memory. The occupied storage unit is not released until the entire program runs to the end. So when the main function calls the print () function, the space still exists. Therefore, the B pointer in the main () function receives the first value of the array and can access the elements in the array. Call Print1 () function: Put array a[n] Change to the pointer *a, and then give the pointer to apply for space, also can run normally. Because when you apply space to pointer A, the space allocated to the pointer is on the heap, and the space on the heap is allocated and released automatically by the programmer. If the programmer does not release,      It may be released by the OS at the end of the program. So the B pointer in the main function can also receive the first value of the space and get the correct answer. Error test.c:30:warning:assignment makes integer from pointer without a cast or test.c:30 when the int *b annotation in the main function is changed to int b[n]: Error:incompatible types when the assigning to type ' int[5] ' from type ' int * ' does not get the correct result for the following reasons: The return value of B in the main function is the first value of the array, and if it is *b, it is  Pointer b points to the first value of the array, so that the pointer variable moves backwards to access all the elements in the array. And if it is b[n], the equivalent of the compiler on the stack to the array b[n] allocated N int space, so B points to the first value of a, cannot pass this address, modify the value of its own application. You can access the source data by moving the pointer backwards through the first value through a pointer. And because B is not initialized , so the result is a random value.PostScript:The purpose of writing this is simply to understand this knowledge through the return value, and of course, the parameter is the best choice.

function return value array

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.