Resolution of C + + pairs of array addresses

Source: Internet
Author: User
Tags integer

In C + + programming, we use arrays in our processes, this looks simple because an array is a collection of elements of the same type, but when you haven't considered the address of an array, everything is simple, and if you touch the address concept in an array, you may change your mind.

Below, I'll list a few examples of what these examples output will look:

First give the definition of the array: int a[10];

1. cout<<a<<endl;

2. cout<<&a<<endl;

3. Cout<<a +1<<endl;

4. Cout<<&a +1<<endl;

Is it a little confusing?

Okay, now, I'm going to one by one to explain these things:

1.cout<<a<<endl;

When we define an array, we give the array a name, called "a", then a refers to the piece of memory that the system assigns to us (40 bytes in this case), so naturally, a represents the starting address of the block of memory.

2. cout<<&a<<endl;

Obviously, this is a means of taking an address to a and still getting the starting address of that block of memory.

3. Cout<<a + 1<<endl;

At this point, we add 1 to the starting address, to determine what the result is, we need to know what type of data a can store, for this example, the integer data stored in a, so it refers to the address value of the second element in a, and the result value of 4 bytes for the starting address plus

4. Cout<<&a + 1<<endl;

This is probably the hardest word to understand, before we get the results, we need to see what a represents, and a represents a memory that can store 10 integer variables, so add 1 to the &a to the next memory that can store 10 integer variables.

Below, give a screenshot of the Code and Effect of the test:

#include <iostream>  
using namespace std;  
      
int main (void)  
{  
      
    int a[10];  
      
    cout<<a<<endl;  
    cout<<&a<<endl;  
    Cout<<a + 1<<endl;  
    Cout<<&a + 1<<endl;  
      
    System ("pause");  
    return 0;  
}

See if the results are the same as I said.

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.