C-language pointer array (each element is a pointer)

Source: Internet
Author: User

Reprint: http://c.biancheng.net/cpp/html/3246.html

Note: The difference between array pointers

if all the elements in an array hold pointers, then we call it an array of pointers . The array of pointers is typically defined as:

DataType *arrayname[length];

[ ]Priority is higher than the * defined form should be interpreted as:

DataType * (Arrayname[length]);

The parentheses inside the description arrayName are an array that contains the length elements, outside the brackets that indicate the type of each element dataType * .

In addition to the different data types for each element, the pointer array and the normal array are all the same in other ways, here is a simple example:

#include <stdio.h>intMain () {intA = -, B =932, C = -; //defines a pointer array    int*arr[3] = {&a, &b, &c};//can also not specify the length, direct writing int *parr[]//defines a pointer to an array of pointers    int**parr =arr; printf ("%d,%d,%d\n", *arr[0], *arr[1], *arr[2]); printf ("%d,%d,%d\n", * * (parr+0), * * (parr+1), * * (parr+2)); return 0;}

Operation Result:
16, 932, 100
16, 932, 100

Arr is an array of pointers that contains 3 elements, each of which is a pointer, and at the same time as ARR is defined, we initialize it with the address of the variable A, B, and C, which is similar to the normal array.

Parr is a pointer to an array of arr, specifically a pointer to the NO. 0 element of Arr, whose definition form should be understood as the int *(*parr) representation in parentheses that * Parr is a pointer to the type of int * data that Parr points to in the outer brackets. The type of the No. 0 element of arr is int *, so you need to add two * when defining a parr.

In the first printf () statement, Arr[i] means to get the value of the element I, which is a pointer and needs to be preceded by an additional * to get the form of the data it points to, that is, *arr[i].

In the second printf () statement, Parr+i represents the address of the element I, * (parr+i) to get the value of the element I (the element is a pointer), * * (parr+i) to get the data that the element I points to.

Pointer arrays can also be used in conjunction with string arrays, see the following example:

#include <stdio.h>intMain () {Char*str[3] = {        "c.biancheng.net",        "C language Chinese network",        "C Language"    }; printf ("%s\n%s\n%s\n", str[0], str[1], str[2]); return 0;}

Operation Result:
C.biancheng.net
C Language Chinese Network
C Language

It is important to note that the character Array Str holds the first address of the string, not the string itself, the string itself in other memory areas, and the character array is separate.

It is only possible to assign a value to the array of pointers as above, except when the type of each element in the pointer array is the char * same.

For ease of understanding, the above array of strings can be changed to the following form, and they are all equivalent.

#include <stdio.h>intMain () {Char*STR0 ="c.biancheng.net"; Char*STR1 ="C language Chinese network"; Char*STR2 ="C Language"; Char*str[3] ={str0, str1, str2}; printf ("%s\n%s\n%s\n", str[0], str[1], str[2]); return 0;}

C-language pointer array (each element is a pointer)

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.