Knowledge points---arrays and pointers

Source: Internet
Author: User

Arrays and pointers are two different types, arrays have a certain number of elements, and pointers are just a scalar value. An array can be converted to a pointer in some cases, and when the array name is used in an expression, the compiler converts the array name to a pointer constant, which is the address of the first element in the array, and the type is the address type of the array element;
The essence of a pointer is an address-related compound type whose value is the location (address) where the data is stored, and the nature of the array is a series of variables;
The array name corresponds to (instead of pointing to) a piece of memory whose address and capacity remain constant over the lifetime, and only the contents of the array can be changed. Pointers can point to any type of memory block at any time, it is characterized by "variable", so we often use pointers to manipulate dynamic memory;
When an array is passed as an argument to a function, the array is automatically degraded to the same type of pointer;

Example 1:
int main (int argc, char* argv[])
{
int a[5] = {1,2,3,4,5};
int *P1 = a + 1;
int *P2 = (int *) (&a+1)-1;
cout << *a << "<< *p1 <<" << *p2 << Endl;
}

The output is: 1 2 5
Explanation: The array name represents the first address of the array, which is the value of the first element in the *a of 1; P1, and 1, which points to the second element, the output 2, and *P2 = (int *) (&a+1)-1; A represents the first address of an array, and &a is a pointer to an array, and &a+1 represents an array size (this is the size of 5 int), which is offset by a-first address, (int *) (&a+1) points to the sixth element of the array, (int *) (&a+1)- 1; points to the fifth element of the array, so the output is 5.

Example 2:
int a[5]={0,1,2,3,4};
If the array name a appears in an expression, such as int *p=a, then it is converted to the address of the first element, which is equivalent to an int *p=&a[0];
NT aa[2][5]={0,1,2,3,4,
5,6,7,8,9};
The array name AA appears in the expression, such as int (*p) [5]=aa; then it is converted to the address of the first element, equivalent to an int (*p) [5]=&aa[0];
The type of aa[0] is int [5], which is an integer array with a number of elements of 5, converted to the address of the first element in the array (int [5]), and the type is int *. So, either int (*p) [5]=aa; or int (*p) [5]=&aa[0];

Example 3:
int *p[n]: pointer array, each element is a pointer to the integer data
int (*p) [n]:p is a pointer to a one-dimensional array that has n integer data
int *p (): function returns pointer, pointer to returned value
Int (*p) ():p as a pointer to a function
int *p[3][4]: An array of 3*4, each element is a pointer to an int
int (*p) [3][4]: A pointer to an int array that points to 3*4
Int (*p[3]) [4]: An array with 3 elements, each of which is a pointer to an int array with 4 elements

Knowledge points---arrays and pointers

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.