Array of pointers (i)

Source: Internet
Author: User
Tags array definition array length arrays integer printf

The definition and use of pointers to different types of variables are described earlier. We can let the pointer point to a class of variables, and replace the variable in the program, we can also let the pointer point to a one-dimensional, two-dimensional array or character array, to replace these arrays in the program to use, give us a lot of convenience in programming.

Here we define a special array, which holds all pointers, which are used to point to variables of a class, in place of the variables used in the program, to increase flexibility. Pointer array definition form:

Type ID * array name [array length]

For example: Char *str[4];

Because [] is higher than the * priority, so the first is the array form Str[4], and then the "*" binding. Thus the pointer array contains 4 pointers s t r [0], S t r [1], S t R [2], S t R [3], each pointing to a variable of the character type. For example:

int *p T R [5];

The pointer array contains 5 pointers p T r [0], p t R [1], P t R [2], P t R [3], p t R [4], and each point to a variable of an integer type.

[Example 6-22] for the application of pointer arrays, we respectively point to the array of strings, point to one-dimensional integer array, and point to a two-dimensional integer array with each pointer in the array of pointers.

#include <stdlib.h>
#include <stdio.h>
m a i n ( )
{
char *ptr1[4]={"china","chengdu","sichuang","chongqin"};
/* 指针数组p t r 1 的4个指针分别依此指向4个字符串* /
int i,*ptr2[3],a[3]={1,2,3},b[3][2]={1,2,3,4,5,6};
for(i=0;i<4;i++)
printf("\n%s",ptr1[i]);/依*此输出ptr1数组4个指针指向的4个字符串*/
printf("\n");
for(i=0;i<3;i++)
ptr2[i]=&a[i];/*将整型一维数组a的3个元素的地址传递给指针数组ptr2*/
for(i=0;i<3;i++)/*依此输出ptr2所指向的3个整型变量的值*/
printf("%4d",*ptr2[i]);
printf("\n");
for(i=0;i<3;i++)
ptr2[i]=b[i];/*传递二维数组b的每行首地址给指针数组的4个指针*/
for(i=0;i<3;i++)/*按行输出*/
printf("%4d%4d\n",*ptr2[i],*ptr2[i]+1);
}

The relationship between the pointer array and the object in the program is shown in Figure 6-12.

The 4 pointers in the PTR1 pointer array point to 4 strings, respectively. As shown in Figure 6-11 (a), this output is in the program; there are 3 pointers to the PTR2 pointer array, and if you pass each element address in the integer one-dimensional array A to each pointer in the pointer array, ptr2[0] points to a[0];ptr2[1 Point to a[1];ptr2[2] and point to a[2]. If you pass the first address of each row in a two-dimensional array to each pointer in an array of pointers, as shown in Figure 6-11b), Ptr2[0] points to line No. 0 of the B array, which has two elements, with the address ptr2[0] and ptr2[0]+1, and the first element of the corresponding pointer array ptr2[i] The two element addresses in line I of point B array are ptr2[i] and ptr[i]+1.

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.