A two-dimensional array of C languages

Source: Internet
Author: User

Two-dimensional arrays and their pointers

1) before we get to the two-dimensional array, let's start by understanding one-dimensional arrays and their pointers

We know that in a one-dimensional array: The array name represents the first address of the first element of the array (never think of the array's first address (&a), absolutely not)

    1. In memory, the code behaves as:</span>

    2. #include <stdio.h> {int a[4];  int *p = A;        Indicates that the pointer points to the first element address of the array, and can of course be written as int *p = &a[0];  return 0; }

650) this.width=650; "src=" http://img.blog.csdn.net/20160801122554483 "style=" border:none; "/>

--At this point, the pointer value (*P) is the data that takes out the value that points to the address &a[0], which is a[0].

---------------------------------- Split Line ------------------------------------

2) below we go to the introduction of a two-dimensional array, first, look at a piece of code:

  1. #include <stdio.h>   

  2. int Main ()

  3. {

  4. int A[2][4] = {1,2,3,4,

  5. 5,,4,5,6};

  6. return  0;

  7. The code behaves like this:

650) this.width=650; "src=" http://img.blog.csdn.net/20160801123323509 "style=" border:none; "/> Right a[ 0] [0] This data corresponds to a two-dimensional array (the elements in each array).

Learn about the representation of two-dimensional arrays in memory:

--we started to introduce the concept of array pointers:


2.1) As the name implies: An array pointer is a pointer to an array, based on the confirmation of a complex type, the array pointer is int (*P) [3]; Indicates that the pointer points to an array ( with three int data ), then the array name A is the first address of the array a[0] , i.e. (&a[0], also can be written as a)

---(this is the same as the array name of the one-dimensional array-----> is the first address of the first element of the array)

(Note: Two different areas of the graph represent two different arrays----followed by the elements contained within each array)


2.2) a[0] This array name represents a[0] The address of the value of a[0] [0] in this array, i.e.----> a[0] = = &a[0 [0]-

For a[0]----understandable (not to draw equals) to a[0] is a pointer (because the array name is the address of the first element a[0] [0], since it is the address, it must be received with a pointer).

650) this.width=650; "src=" http://img.blog.csdn.net/20160801124549432 "style=" border:none; "/>

2.3)p = A; is the first address of the array pointing to a[0].  Can also be written as &a[0]; A row pointer named a two-dimensional array

Then, if P +1 is performed on the page , the address is moved to the next line (sizeof (int) * = 16 bytes ), which is the address of the next array-as shown

Technique--/* We treat the two-dimensional array in turn as a one-dimensional array stepwise analysis */

2.4) so p = a + i ; Equivalent to p = &a[0] + i

This is the concept of the line pointer, if p is taken to the * operator, the content of P point is removed, and p points to the content is a[0], the row pointer is used to determine which array of values to operate, intuitively, this two-dimensional array, select a[0], a[1] Two of the In one of the operations),

2.5) *p = *( &a[0] )= a[0] , * (p + i) = a[i](c, square brackets [] operator and * () operator are the same, interchangeable)

a[0 ] + i = = &a[0 [0] + i is the concept of the column pointers (operations on each element of the array), the address of the element inside the a[0] is the address + I, referring to the corresponding element a[0] [i] addresses that are &a[0] [ I] wealready knowa[0]is thata[0 [0]the address of the value, i.e. &a[0] [0] so if you take the * operator for a[0] , you get the value of a[0 [0] .

that * ( a[0]+ i ) = a[0 [i]

-------------------------------------- See these do not know to friends have no help.

Here I do a drawing analysis of level two pointers: Let's take a deeper understanding of the two-dimensional array pointers

650) this.width=650; "src=" http://img.blog.csdn.net/20160801133454219 "style=" border:none; "/>

       --------------------------------------------------------------------------------------------- ------

In the article what is wrong or need to improve the place, please friends to me to give valuable comments and suggestions




This article is from the "Tao Sound Still" blog, please make sure to keep this source http://418200329.blog.51cto.com/8214140/1858947

A two-dimensional array of C languages

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.