C-language array of pointers and pointers to pointers

Source: Internet
Author: User
Tags constant

Array of pointers

An array of pointers is a special kind of array that holds all the memory addresses of the same data type. The pointer array is defined in the form:

Data type * array name [length];

For example:

const char *c[4] = {"We", "USA", "Rassia", "Japan"}; Defines a constant-character pointer array of length 4, pointing the array element to 4 string constants, respectively

int i;
for (i = 0; i < 4; i++)
{
Puts (* (c + i)); The string that the array-pointer array element points to
}

A pointer to a constant must be defined with const as a constant pointer to avoid modifying the data pointed to by the pointer to cause a program error. Because [] the symbol is higher than the indirect operator *, the first is the array form c[4], then the * binding, so that the pointer array contains 4 pointers c[0], c[1], c[2], c[3, respectively, pointing to the first address of 4 strings.



pointer Variable

A pointer variable can point to another pointer variable, rather than passing the memory address of a pointer variable to another pointer variable, defining a pointer variable that points to a pointer type, which can be called a double pointer. The dual pointer is defined in the form:

Data type * * variable name;

It uses 2 indirect operators, as shown in the following example:

int I, *PI, **dpi; Declare integer variable i, integer pointer variable pi and integral type double pointer variable dpi
PI = &i; Assigning the address of the variable i to the integer pointer variable pi
DPI = &pi; Assigns an integer pointer variable pi address to the integer double pointer variable dpi
**dpi = 100; Indirectly referencing the variable i, assigning the variable i
printf ("%d", I); Value of output variable i

The double pointer variable dpi is defined in the code, and the double pointer variable to the pointer variable is the memory address of the pointer variable itself must be obtained using the address operator. By indirectly referencing a variable that is pointed through a dual pointer variable, you need to complete the double operation with 2 address operators because you want to get the address of the pointer variable first and then obtain the memory address that is saved in the pointer variable.

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.