C-language Array detailed

Source: Internet
Author: User
Tags array length arrays constant printf what array

  This article mainly introduces the C language array, a friend in need can refer to the

What is an array name   array is a contiguous amount of memory available. For example, declare an int array   int array[]={1,2,3}; What does   array represent? Some data say that an array name is a constant pointer to the first address of an array.     Below we can verify. I know that the sizeof operator can return the number of bytes of memory that an object or type occupies. such as: int i=1; Then the result of sizeof (i) is 4 (part of the compiler under 64-bit machine is 8)   that we print sizeof (array)     printf ("%dn", sizeof (array));   results are: 12.   But we all know sizeof (pointer variable) ==4.     All we come to: the array name is not exactly the constant pointer to the first address of the array.   Why use incomplete, because we use arrays to access array elements. It becomes like a constant pointer again.   For example Array[0] is equivalent to * (array+0)   At this point array is a constant pointer to the first address of the array, and the pointer type is a pointer to the type of the array element. This is the int* type   We can understand this:   A university is named array. Someone asked you what array is. You will tell him that the array is the university, the area and so on. But when someone asks you how to get to array, you tell him where the school gate (first address) of the array is.   Conclusion: The array name actually represents an area of memory, but when used it becomes a constant pointer to the first address of the array.   But here's a little trap: The   code is as follows: #include <stdio.h>   void foo (int a[]) {printf ("%dn", sizeof (a));}   I NT Main (void) {int array[]={1,2,3};     foo (array);     return 0;}       output is not 12, but 4.   For efficiency reasons, array passes are references to arguments instead of copy passes. Because the array length can be very large, a copy of the words too much resources. Although I am so the function is this     Code as follows: void foo (int a[]) {printf ("%dn", sizeof (a));}     compiler in the eyes of this       code is as follows: void Foo (int *a) {printf ("%dn", sizeof (a));}     so sizeof (a) is sizeof (pointer variable) is definitely 4;   Two character array   First we look at a simple program     code as follows: # include <stdio.h>   int main (void) {char *str1= "abc"   ; Char str2[]= "DEF";     printf ("%sn", str1+4); return 0; The result of the       output is def.     We want to know the C language as long as the use of "xxxxx", the system will automatically add double quotes to the Word constants volume area. Note: printf ("xxxx") does not add "xxxx" to the character constants area.     Code as follows: Char *str1= "ABC";        //will add ABC to the character constants area and assign the first address to the STR pointer variable. Char str2[]= "DEF";        //will add def to the constants volume area, and adding a character array to the function stack also def,str2 points to an array in the stack. Char str[]={' x ', ' y ', ' z '}; Only add an array to the function stack     because the character constants volume area is continuous, so   printf ("%sn", str1+4);     You can print out the STR2 value.   32-D array   int array[][3]={1,2,3,4,5,6};   We've already said that when you use an array to access an element, the array is a pointer type that points to an array element, pointing to the pointer to the first address of the array. The elements of a two-dimensional array are arrays,   writeEasier to understand: int array[][3]={{1,2,3},{4,5,6}};   All can think so that the array is so considered int (*const array) [3]; When I access the array element     Array[x][y] The compiler appears to be * (* (array+x) +y   * (array+x) with an array of x row Type "Int[3" (C language does not have such a notation), &N Bsp The array name is used as the first address pointer when accessing the element, where * (array+x) is equivalent to the array name, and the pointer type int *, pointing to the address array+sizeof (int (*) [3]) *x.   to access the first Y element of the array, use the * (* (array+x) +y).   These are my understanding of the C language array, if the wrong place, thank you, light spray ha.  
Related Article

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.