C Language Notes (two-dimensional array and numerical pointer), two-dimensional array pointer

Source: Internet
Author: User

C Language Notes (two-dimensional array and numerical pointer), two-dimensional array pointer

I. Differences between two-dimensional arrays and two-dimensional arrays

(1) One-dimensional arrays are stored continuously in the memory. Likewise, two-dimensional arrays are stored continuously in the memory. Therefore, from the perspective of memory, there is no essential difference between a one-dimensional array and a two-dimensional array.

(2) Two-dimensional arrays can be replaced by one-dimensional arrays. However, in practical applications, it is more intuitive to use a two-dimensional array to facilitate program programming.

(3) The two have the same memory usage efficiency.

 

2. concepts of the first dimension and the second dimension of a two-dimensional array

(1) For example, In int a [2] [5], 2 indicates the first dimension; 5 indicates the second dimension.

(2) The first dimension of a two-dimensional array represents the most external layer. The first dimension is an array and contains two elements, which are the second-dimensional arrays. The second-dimensional array is also an array. The elements in the array are common int variables.


3. subscript access to two-dimensional arrays

Example 1:

1 int a [2] [5] = {{1,2,3,4,5}, {6,7,8,9,10}};
2 int (* p) [5]; // define an array pointer
3 p = a;
4
5 printf ("a [1] [2] =% d. \ N", a [1] [2]); // a [1] [2] = 8
6 printf ("(* (p + 1) +1) =% d. \ N", * (* (p + 1) +2)); // a [1] [2]

Running result:


 

4. symbols that must be understood about two-dimensional data

Example 2: Understanding a, & a, a [0], & a [0], a [0] [0], & a [0] [0]

1 /*
  2 Testing of several symbols in a two-dimensional array
  3 1, a is equivalent to & a [0]
  4 2. a [0] is equivalent to & a [0] [0]
  5 3. In terms of value, a, & a, a [0], & a [0], & a [0] [0] are equal, but there are differences in types.
  6 * /
  7 int a [2] [5] = {{1,2,3,4,5}, {6,7,8,9,10}};
  8     
  9 printf ("a =% p. \ N", a); // a is of type int (*) [5]
10 printf ("& a =% p. \ N", & a); // & a is of type int (*) [2] [5]
11 printf ("a [0] =% p. \ N", a [0]); // a [0] is of type int *
12 printf ("& a [0] =% p. \ N", & a [0]); // & a [0] is of type int (*) [5]
13 printf ("a [0] [0] =% d. \ N", a [0] [0]); // a [0] [0] is of type int
14 printf ("& a [0] [0] =% p. \ N", & a [0] [0]); // & a [0] [0] is of type int *

Running result:

 

Example 3: Use of array pointers in dimension 1 and dimension 2

1 // Combination of two-dimensional array and pointer
  2 int a [2] [5] = {{1,2,3,4,5}, {6,7,8,9,10}};
  3
  4 int (* p1) [5]; // array pointer
  5 int * p2; // general pointer
  6 p1 = a; // equivalent to p1 = & a [0]; // array name pointing to a two-dimensional array
  7 p2 = a [0]; // equivalent p2 = & a [0] [0]; // first-dimensional array pointing to two-dimensional array
  8 
  9 printf ("a [0] [2] =% d. \ N", * (* (p1 + 0) +2)); // a [0] [2] = 3
10 printf ("a [1] [2] =% d. \ N", * (* (p1 + 1) +2)); // a [1] [2] = 8
11
12 printf ("a [0] [2] =% d. \ N", * (p2 + 2)); // a [0] [2] = 3
13 printf ("a [0] [4] =% d. \ N", * (p2 + 4)); // a [0] [4] = 5 

Running result:

 

V. Summary

(1) understand the essence of a two-dimensional array and the meanings of several symbols.

(2) Two-dimensional arrays and array pointers are closely related. Learn to use array pointers to operate two-dimensional arrays, and focus on practical operations to deepen understanding.

 

Note: The study notes are extracted from the IOT video tutorial by instructor Zhu youpeng.


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.