faressoft
faressoft

Reputation: 19651

Are arrays a const pointer in C++?

Is it true that C++ array is a const pointer in C++ ?

http://www.cplusplus.com/forum/articles/10/#msg1381

So why is x and &x the same if they are pointers?

#include<iostream>
using namespace std;

int main() {
    int x[5];
    int *y = new int[5];

    cout << "&x : " << &x << endl;
    cout << "x : " <<  x << endl;
    cout << endl;
    cout << "&y : " << &y << endl;
    cout << "y : " <<  y << endl;
    return 0; 
}

Output:

&x:  0xbfec4ccc
x :  0xbfec4ccc

&y:  0xbfec4ce0
y :  0x8961008

Link to ideone of above code: http://ideone.com/3JRZd

Upvotes: 0

Views: 185

Answers (5)

molbdnilo
molbdnilo

Reputation: 66371

Silly analogy:

Consider the address 1, Main Street.
It is not the same thing as the street "Main Street", despite the fact that you can refer to the beginning of Main Street as "1, Main Street". In fact, if you want to meet someone at that end of the street, you can give them a note with the address "1, Main Street" on it as a pointer to where to wait for you, which is much easier than fitting all of Main Street onto a bit of paper (or giving them a map with an "X" on it, if we allow ourselves to introduce an abstraction layer).

1, Main Street is of course not only the address of the beginning of Main Street but also the address of the first house on Main Street - this is what your printing of x shows - but neither a house nor an address is a street.

Upvotes: 1

celtschk
celtschk

Reputation: 19721

No, it is not true that an array is a const pointer in C++. Indeed if one file contains

extern int* const a;

and the other contains

int a[10];

in the same scope, combining both files will cause strange errors.

However, arrays decay into pointers in most situations. More exactly, if you use an array in an rvalue context, it is implicitly converted into a pointer to its first element. This is what happens to x in the example in the second link: Since the first element of the array of course is at the same address as the array, both x and &x are printed as the same address. However note that they are not the same pointer because they are of different types: x in this context decays into int*, i.e. pointer to int, while &x is of type int(*)[5], i.e. pointer to array of 5 ints.

Another case where you can see that both are different is a function taking a reference to an array, like

void f(int (&arr)[5]);

You can pass x to this function (because it is of type int[5]) but neither &x nor the pointer y you get when writing int* y = x;

Upvotes: 3

Joran Beasley
Joran Beasley

Reputation: 113978

yes they are.

because a pointer is always an address so the &value should be the same for any type of pointer.

also you can do something like this

int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int *pNumbers = number;

[edit] maybe not per the other answers ... but i was always under the impression this was true [edit] also see http://www.cplusplus.com/doc/tutorial/pointers/ under "pointers and arrays"

Upvotes: -1

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

The code

int array[3];

is NOT the same as

int const *array=new int[3];

just for simple, but not single reason: you will need to call delete [] array in the second case.

Upvotes: 1

GManNickG
GManNickG

Reputation: 503855

Graham is wrong and doesn't know what he's talking about. Arrays and pointers are two different things.

Upvotes: 4

Related Questions