Address offset in the C language Array

Source: Internet
Author: User

In C/C ++, address offsets in one-dimensional arrays and two-dimensional arrays are discussed.

One-dimensional array int a [3];

Two-dimensional array int a [3] [3];

 


1. first look at the situation of one-dimensional arrays:


[Cpp]
# Include <iostream>
 
Using namespace std;
 
Int main ()
{
Int a [3] = {1, 2, 3 };

Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
 
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
 
System ("pause ");
Return 0;
}

# Include <iostream>

Using namespace std;

Int main ()
{
Int a [3] = {1, 2, 3 };
 
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;

Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;

System ("pause ");
Return 0;
}

 

The result shows that & a, a, and & a [0] indicate the same address, but the level is different.

The & a + 1 Address and & a offset is 12 bytes, indicating the space size of the array;

The a + 1 address is 4 bytes different from the address, that is, the space size of an element in the array;

The address & a [0] + 1 is 4 bytes offset from the address & a [0], that is, the space size of an element in the array;


That is to say, & a, a, and & a [0] indicate the same address, but the compiler will differentiate them. & a points to the address of the entire array, which is the most advanced address in the array, a and & a [0] indicate that & a represents the next level of address.

 
 


2. Two-dimensional array


[Cpp]
# Include <iostream>
 
Using namespace std;
 
Int main ()
{
Int a [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
 
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a [0] [0] <endl;
Cout <a [0] [0] <endl;
 
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
Cout <& a [0] [0] + 1 <endl;
Cout <a [0] [0] + 1 <endl;
 
System ("pause ");
Return 0;
}

# Include <iostream>

Using namespace std;

Int main ()
{
Int a [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};

Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a [0] [0] <endl;
Cout <a [0] [0] <endl;

Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
Cout <& a [0] [0] + 1 <endl;
Cout <a [0] [0] + 1 <endl;

System ("pause ");
Return 0;
}

 
 


Result Analysis:

& A, a, & a [0], a [0], and & a [0] [0] indicate the same address, but the level difference is large.


The declared int a [3] [3] is 36 bytes in size.


The & a + 1 Address and & a offset is 36 bytes, indicating the space size of the array;

The a + 1 address is a-byte offset of 12 bytes, that is, the space size of a row of elements in the array;

The address & a [0] + 1 is offset by 12 bytes compared with & a [0], that is, the space size of a row of elements in the array;

The address a [0] + 1 is 4 bytes different from the address a [0], that is, the space size of an element in the array;

& A [0] [0] + 1 address is 4 bytes offset compared with & a [0] [0], that is, the space size of an element in the array;

As can be seen from the above, & a is the highest level of address, and a + 1 and & a [0] + 1 are the second level of address, a [0] + 1 and & a [0] [0] + 1 represent the third-level address in a two-dimensional array.


 

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.