My personal understanding about pointer arrays and pointer arrays in C language, pointer Arrays

Source: Internet
Author: User

My personal understanding about pointer arrays and pointer arrays in C language, pointer Arrays

I. pointer array and pointer Array

  1. pointer Array

As the name suggests, an element is an array of all pointers. Its form is similar to that of a common array, for example, * a [N].

Before understanding how to use pointer arrays, let me explain my personal understanding of arrays.

For example, a one-dimensional integer array (such as int a [3]) is actually a variable with three integer elements; a two-dimensional integer array (such as int a [4] [3]) can be considered as composed of a [4] and int [3, consider a [4] as a one-dimensional array, which contains a [0], a [1], a [2], a [3], each element is of the int [3] type. For example, a [0] contains a [0] [0], a [0] [1], a [0] [2] Three elements; 3D arrays and so on...

Since the element of the pointer array is a pointer, each element of the pointer array can point to other elements of the same type (for example, to an array ).

The following example shows how to use a pointer array.

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 # define M 4 5 # define N 3 6 7 void main () 8 {9 int * a [M], B [M] [N]; // define the pointer array and the two-dimensional array respectively. 10 int I, j; 11 12 for (I = 0; I <M; I ++) // assign values to the array 13 {14 for (j = 0; j <N; j ++) 15 {16 B [I] [j] = I + j; 17} 18} 19 20 for (I = 0; I <M; I ++) // assign a value to the pointer array, point the element to element 21 {22 a [I] = B [I]; 23} 24 25 for (I = 0; I <M; I ++) // print the array 26 {27 for (j = 0; j <N; j ++) 28 {29 printf ("% 4d ", a [I] [j]); // a [I] [j] can be written as * (a [I] + j) other reasonable formats such as 30} 31 printf ("\ n"); 32} 33 34 35 getchar (); 36}
View Code

 

2. array pointer

An array pointer is a pointer to an array in the form of (* a) [N]. It can point either to a one-dimensional array or to a two-dimensional array. When the + 1 operation is performed, it will span N Units.

The following is an example.

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define M 4 4 # define N 3 5 6 7 void main () 8 {9 int (* a) [N], B [M] [N]; 10 int I, j; 11 12 for (I = 0; I <M; I ++) 13 {14 for (j = 0; j <N; j ++) 15 {16 B [I] [j] = I + j; 17} 18} 19 20 21 printf ("Print array \ n" Using B [I] [j]); 22 for (I = 0; I <M; I ++) 23 {24 for (j = 0; j <N; j ++) 25 {26 printf ("% 4d", B [I] [j]); 27} 28 printf ("\ n"); 29} 30 31 a = & B [0]; // or a = B, but the former may be more accurate 32 33 printf ("using a [I] [j] to print the array \ n"); 34 for (I = 0; I <M; I ++) 35 {36 for (j = 0; j <N; j ++) 37 {38 printf ("% 4d", a [I] [j]); 39} 40 printf ("\ n"); 41} 42 43 getchar (); 44}
View Code

 

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.