Pointer array/array pointer, pointer Array

Source: Internet
Author: User

Pointer array/array pointer, pointer Array

# Include <iostream> using namespace std; // note how pointer arrays and array pointers point to two-dimensional arrays # include <stdio. h> main () {static int m [3] [4] = }; /* define a two-dimensional array m and initialize */int (* p) [4]; // array pointer p is a pointer pointing to a one-dimensional array, each one-dimensional array has four int elements: int I, j; int * q [3]; // the pointer array q is an array, and the array element is a pointer, the three int pointers p = m; // p are pointers, which can direct to the two-dimensional array printf ("-- array pointer output element -- \ n"); for (I = 0; I <3; I ++)/* outputs the values of each element in a two-dimensional array */{for (j = 0; j <4; j ++) {printf ("% 3d", * (p [I] + j); // printf ("% 3d", * (p + I) + j);} printf ("\ n");} printf ("\ n"); for (I = 0; I <3; I ++, p ++) // p can be viewed as a row pointer {printf ("% 3d", ** p); // the first element of each row printf ("% 3d ", * (* p + 1); // the second element of each row, printf ("% 3d", * (* p + 2 )); // printf ("% 3d", * (* p + 3), the third element of each row )); // printf ("\ n");} printf ("\ n"); printf ("\ n"); printf ("-- pointer array output element -- \ n "); for (I = 0; I <3; I ++) q [I] = m [I]; // q is an array, element q [I] is a pointer for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) {printf ("% 3d", q [I] [j]); // q [I] [j] can be changed to * (q [I] + j )} printf ("\ n");} printf ("\ n"); printf (""); q [0] = m [0]; for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) {printf ("% 3d ", * (q [0] + j + 4 * I);} printf ("\ n");} printf ("\ n ");}

Output result:

This code is well written .....

Related Article

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.