Examples look at two-dimensional arrays, pointers, two-dimensional array pointers

Source: Internet
Author: User

Routines:

/******************************************************** file Name: Routine * * File Description: Example of two-dimensional array, pointer, two-dimensional array pointer * * Creator: jesse** version number: * * Modify Record: /#include <stdio.h> #define ROW  3#define Line 3void Main (void) {const int Array[row][line] = {//array is a pointer to int [line] {11,12,13},{21,22,23},{31,32,33}};unsigned  Char row, line;const int *pr = Null;//int *PR is a pointer to int *, const int (*ARRPR) [Line] = Null;//int (*ARRPR) [row] is a pointer to int A pointer to [line], with an array of the same type const int **DPR = Null;//int **DPR is a pointer to the int*, same as &PR. Two-dimensional array value printf ("///              two-dimensional array value: Array[row][line]              //\n"); for (row=0; row<row; row++) {for (line=0; line<line; line++) printf ("row:%d,line:%d,value:%d    ", row, line, Array[row][line]);p utchar (' \ n ');}}
The results are printed as follows:


Here we use a single-layer pointer to output the values of the two-dimensional array:

The pointer and the two-dimensional array value PR = array;//force the type of array (int [line]) to be converted to the type (int *). PS: When you add 1, you add sizeof (int) instead of 3*sizeof (int). for (row=0; row<row; row++) {for (line=0; line<line; line++) printf ("row:%d,line:%d,value:%d    ", row, line, * (pr +row*3+line));p Utchar (' \ n ');}

PR = array; The double-decker pointer is cast to a single-level pointer, which is the PR-plus 1 plus the sizeof (int), and because the array is stored in a contiguous stack, the value of the array can be output with * (Pr+row*3+line).


Here we use a two-dimensional array pointer to output the values of a two-dimensional array:

Two-dimensional array pointers and two-dimensional array values ARRPR = ARRAY;//ARRPR with an array of the same type of pointer printf ("//              two-dimensional array pointer and two-dimensional array: arrpr[row][line]              //\n"); for (row = 0; row<row; row++) {for (line=0; line<line; line++) printf ("row:%d,line:%d,value:%d    ", row, line, Arrpr[row][line]);p Utchar (' \ n ');}

ARRPR = array; Equivalent to assigning a two-dimensional array pointer to another two-dimensional array pointer. So ARRPR can be used like an array: Arrpr[row][line]


Here we use a two-level pointer to output the values of the two-dimensional array:

Two-level pointers and two-dimensional array values//method One: printf ("//              two-layer pointer and two-dimensional array: * (*dpr+line)              //\n"); for (row=0; row<row; row++) {PR = Array[row]; DPR = &pr;for (line=0; line<line; line++) printf ("row:%d,line:%d,value:%d    ", row, line, * (*dpr+line)); Putchar (' \ n ');} Method Two: printf ("//              two-layer pointer and two-dimensional array: **DPR              //\n"); for (row=0; row<row; row++) {for (Pr=array[row]; Pr-array[row] <LINE; pr++) printf ("row:%d,line:%d,value:%d    ", row, line, **DPR);p Utchar (' \ n ');}
I tried it before.

DPR = array;    Force the type of array (int [line]) to be converted to (int *)
Whether using the PR output method output, or using the output method of ARRPR output, the results obtained are wrong.

A variable of type int **DPR cannot directly accept the address of a two-dimensional array, and for a two-dimensional array, DPR is equivalent to a pointer of type int (*ARRPR) [] and does not indicate how many int types are included, which is an error.

So the first level of the pointer must indicate the width:

PR = Array[row];
The second-level pointer then points to the first-level pointer:

DPR = &pr;
And then in use:

* (*dpr+line)
The output array.


Examples look at two-dimensional arrays, pointers, two-dimensional array 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.