The difference between array name A and &a in C + + learning

Source: Internet
Author: User

Define an array in C + +:

int a[5]={1,2,3,4,5}
So what does a really mean? The first thought is that a represents the first element address of an array. So what if you ask what type A is? A lot of people are not sure, look at the following procedure, let the compiler tell us what kind of data A is:

Main.cpp as follows:


From the error message in the picture, you can see:

The type of a is an int [5] type. It is not possible to assign a pointer to an int (*) [5] type directly, so you need to take an address action on a &a the type that is given is the INT (*) [5] Type (pointer type that points to the int [5] type).

So why is a in the picture and can be directly assigned to an int * type variable p3? This is because the int [5] type is implicitly converted at assignment, an int [5] type of a is converted to a variable of type int *, stating the int[5] type and int * The type is very similar to the data type. And we know &a[0] The data type is int * Type, so it is generally said that the array name A is the address of the first element represented, in fact there is a certain difference, the specific difference is not sure.

Based on the above analysis, a and &a are actually two different data types, a is an int [5] type (like int *, which can be understood as a pointer to the first address of a 5-element array), and &a is an int (*) [5] Type ( Can be understood as pointing to a pointer to an array of 5 child elements), note the difference between the whole and the individual.

So if we perform +1 operations on a and &a respectively, the results are different.

#include <iostream> #include <string> #include <vector> #include "Dog.h" using namespace Std;int main ( int argc, const char * argv[]) {    int a[5]={0,1,2,3,4};    cout << "A:" << a << "<< a+1 <<endl;    cout << "&a:" << &a << "<< &a+1 <<endl;    return 0;}
The output is:



Can see a words +1 is actually added an int size 4 bytes, and &a+1 Plus is the entire array of memory size, which also reflects a and &a individual and the difference between the whole. This should be the result of a different type overloaded + number operator, which, like an int [5] Type overloaded + number operator, is the size of an int, and the int (*) [5] Type is the size of the 5 int type.


The difference between array name A and &a in C + + learning

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.