The essence analysis of the 27th class array

Source: Internet
Author: User

1. The concept of arrays

(1) An array is an ordered set of variables of the same type

(2) array to store elements in a contiguous memory space

(3) The number of array elements can be displayed or implicitly specified

Initialization of the "Programming Experiment" array

#include <stdio.h>intMain () {//Array Initialization    inta[5] = {1,2};//1th, 2 elements are 1, 2, the remainder is 0    intB[] = {1,2};//During compilation, the compilation will be the size of the computed array b        intI=0;  for(i =0; I <4; i++) {printf ("a[%d] =%d\n", I,a[i]); } printf ("sizeof (a) =%d\n",sizeof(a));// -printf"sizeof (b) =%d\n",sizeof(b));//8printf ("count for a:%d\n",sizeof(a)/sizeof(int));//5printf"count for B:%d\n",sizeof(b)/sizeof(int));//2    return 0;}

2. Array address (&a) and array name a

(1) The array name a represents the address of the first element of the array. Therefore, the address of the 2nd element is a+1, and so on .... Note A or a+i represents the address of the element. You can use * (a+i) to take out the value of the element, or you can use A[i] to remove the value of the element, because when the compilation encounters A[i] it automatically turns to * (A+i). In turn, the address of the 1th element is either a or &a[0], the address of the 2nd element is a+1 or &a[1], and the address of the element i is (a+i) or &a[i] ...

(2) the address of the array needs to be taken with the address & . The shape, such as &a, takes the address of the entire array, so &a+1 represents the position of the last face of the entire array.

(3) The address value of the first element of the array is the same as the address value of the array, but two different concepts .

"Programming Experiment" array names and arrays addresses

#include <stdio.h>intMain () {//initializes each element of the array to 0    inta[5] = {0};//meaning, initialize the 1th element to 0 and the remainder to 0.printf ("A =%p\n", a);//address of the first elementprintf"&a =%p\n", &a);//The address of the entire array, in terms of values and looks, is the same as a. printf"&a[0] =%p\n", &a[0]);//address of the 1th element    return 0;}

3. blind spots of array names

(1) The connotation of the array name is that its reference entity is a data structure, and this data structure is an array. such as int a[5] indicates that the type of a is int[5], so sizeof (a) represents the size of the entire array, &a represents the address of the array.

(3) Extension of array name : In addition to sizeof (a) and &a , the array name is often considered to be a constant pointer . Be aware, however, that this is only "considered", not a real pointer . Unlike pointers, the array name is just a symbol in the compilation process, and the compiler does not allocate memory for it, some call it "pseudo-variable". Therefore, the form a++\a-or a=b (where B is another array name) These are all wrong, because a is just a symbol, the compiler will put the array information (such as size, address) into the symbol table, each time you encounter the array name A, the address of the array will be taken from the symbol table, and then use this fixed address instead of a, So this symbol is not allocated memory space, and the above operations are for variables, so the array name can only be used as the right value.

(4) a reference to an array , such as a[i] or * (a+i), requires only one access to memory , and a pointer reference such as * (P+i) takes two times , preferred to find the P pointer by &p , then add I, and then remove the contents from the P+i.

(5) When the array name is a formal parameter , it is degraded to a pointer . That is, you can use the array name as a pointer, where sizeof (the array name) is 4, which is the length of the pointer.

"Instance analysis" array and pointers are not the same

#include <stdio.h>intMain () {//initializes each element of the array to 0    inta[5] = {0}; intb[2]; int* p =NULL; P=A; printf ("A =%p\n", a);//address of the first elementprintf"p =%p\n", p);//P==a. printf"&p =%p\n", &p);//the address of the pointer pprintf ("sizeof (a) =%d\n",sizeof(a));//size of the array:printf"sizeof (P) =%d\n",sizeof(p));//the size of the pointer is 4.printf ("\ n"); P=b; printf ("B =%p\n", b);//address of the first elementprintf"p =%p\n", p);//p==b. printf"&p =%p\n", &p);//the address of the pointer pprintf ("sizeof (b) =%d\n",sizeof(b));//size of the array: 8printf"sizeof (P) =%d\n",sizeof(p));//the size of the pointer is 4. //a = b;//compile error, array name cannot be left value; //a++; //compile error, array name is compiled with a fixed address, equivalent to 0xaabbccdd++ error    return 0;}

4. Summary

(1) array is a contiguous memory space

(2) Address of the array and the address of the first element have different meanings

(3) Array names are treated as constant pointers in most cases

(4) The array name is not a pointer, it cannot be equated to a pointer.

The essence analysis of the 27th class 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.