Array pointer and pointer Array

Source: Internet
Author: User

The array pointer and pointer array are both arrays of deformation. See the following three representations:

1>    char   a[6]=“123456”;2>    char * a[6]={“12”,“34”,“56”,“78”,“910”,“1011”};3>    char(*a)[6];  char str[2][6]={"123456","234567"}; a=str;

In 1, A is an array containing six char elements, and a is the array name.

In 2, A is a pointer array. First, it is an array. The array elements are char pointers that point to strings.

In 3, A is an array pointer. First, it is a pointer. pointer A points to a char array with a length of 6.

It can be understood as follows:

The comparison between 1 and 2 is equivalent to replacing char with char *. Originally, char is placed in a, and char * is now placed *.

The comparison between 1 and 3 is equivalent to replacing a with * A. The original array name is a, And now it becomes * A, which is a pointer.

Whether it is an array pointer or a pointer array, the key is whether a is replaced or char is replaced. If a is replaced, it becomes a pointer and an array pointer. The Char is replaced with an array, but an array with a pointer.

The following code is used:

# Include <iostream> using namespace STD; int main (void) {char STR [100] = "I love China! "; Cout <" array STR: "<STR <Endl; char * A [3] = {" I "," love "," China! "}; // Pointer array, which is an array, but put the cout pointer to char in the array <" pointer array a is: "; for (INT I = 0; I <3; I ++) {cout <A [I] <"" ;}cout <Endl; cout <"the array Pointer Points to the following array :"; char (* P) [6]; // array pointer, which is a pointer to an array of 6 char S2 [3] [6] = {"I ", "Love", "China"}; P = S2; For (INT I = 0; I <3; I ++) {cout <p [I] <"" ;}cout <Endl; getchar ();}

 

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.