On the difference between an array as a parameter and the array name and the address of a logarithmic group

Source: Internet
Author: User

  • Here's another look at what's different for arrays A,a and &a
    1#include <stdio.h>2  3  voidChange_array (Char*,int);4  5  intMainvoid)6  {7      Chara[Ten] ="ABCDEFG";8printf"&the address of origin a =%p\n",&a);9printf"A1:%p\n", a); Tenprintf"sizeof (&a):%d\n",sizeof(&a)); Oneprintf"sizeof (a)%d\n",sizeof(a)); AChange_array (A,Ten); -printf"&the Address of later a =%p\n",&a); -printf"A2:%p\n", a);  theprintf"%s\n", a); -      return 0; -  } -   +  voidChange_array (Char*a,intN) -  { +scanf"%s", a);//to my surprise, this gets the same result as using either a or &a. Aprintf"arraysizeof (&a):%d\n",sizeof(&a)); atprintf"arraysizeof (a):%d\n",sizeof(a)); -printf"&array:%p\n", &a); -printf"array1:%p\n", a);  -  } -   -  /*************************************************** in * &the address of origin a = 0x7ffee2cdd0a0 - * a1:0x7ffee2cdd0a0 to * sizeof (&A): 8----------> In this case, &a corresponding byte is 8, in my Computer, the pointer p address (&P) to get the size is also 8 + * sizeof (a)----------the corresponding byte of >a is 10, indicating that the size of a is a[10], that is, the size of the array.  - * xxxx the * arraysizeof (&a): 8----------> here is 8, no doubt, indicating the pointer size is 8 * * Arraysizeof (a): 8---------------------->>>>>>>>>> Here's a question. Put your face down and say!!! $ * &array:0x7ffee2cdd088Panax Notoginseng * array1:0x7ffee2cdd0a0 - * &the Address of later a = 0x7ffee2cdd0a0 the * a2:0x7ffee2cdd0a0 + * xxxx A   * *************************************************/

    Then the code in 35 lines: through the above output, we know that the Mian in the array A and the Array_change in the array a address is the same! That is 0x7ffee2cdd0a0, note A and & A is different. So why is sizeof (a) in main the size of 10 bytes, and sizeof (a) in Array_change is only 8 bytes in size? It is verified that when the array name is passed as a parameter, only the array address is passed. Does not pass the size of the array together, so we need to define another parameter to pass the array size.

  • to prove that the size of the pointer itself is 8, I write a program (the size of the pointer itself may be different in different systems)
    1#include <stdio.h>2  3  intMainvoid)4  {5      Char*PA =NULL;6      CharA ='Q';7PA = &A;8printf"sizeof (a):%d\n",sizeof(a));9printf"sizeof (&a):%d\n",sizeof(&a));Ten      return 0; One   A  } -   -  /***************************** the * sizeof (a): 1 - * sizeof (&A): 8 -   * ***************************/

On the difference between an array as a parameter and the array name and the address of a logarithmic group

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.