Arrays are pointers; pointers are arrays?

"Arrays are pointers; pointers are arrays."

What does this means on a conceptual level? I've heard my professor talk about arrays in this way but I don't understand.

Can anyone give an example or two of how this is true on a practical level?

Thanks in advance
It's technically not true (there are some subtle differences), but it might help to think of them that way.

The point your professor is trying to get across is that arrays and pointers can be used with the same syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
// an array
int myarray[10];

// how you use an array:
myarray[0] = 15;
myarray[1] = myarray[0] + 3;

// a pointer
int* ptr = &myarray[0]; // points to the first element in myarray

// how you can use a pointer
ptr[0] = 15;
ptr[1] = ptr[0] + 3;
> "Arrays are pointers; pointers are arrays."

No. Arrays are arrays and pointers are pointers, though the two are closely related. In the long run, it doesn't help at all to think of them in any other way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

int main()
{
    typedef double array_type[20] ; // type array_type is: an array of 20 doubles
    typedef double* pointer_type ; //  pointer_type is a pointer to a double

    std::cout << sizeof(array_type) << '\n' ; // prints trhe size of an array of 20 doubles
    // which is equal to 20 * sizeof(double)

    std::cout << sizeof(pointer_type) << '\n' ; // prints the size of a pointer to double

    array_type a ; // a is an array of 20 doubles
    double d = 8.7 ;
    pointer_type p = &d ; // p is a pointer to double; it now points to d

    // an array when used in a context where a value is required is implicitly
    // converted to a pointer to its first element
    p = a ; // p now points to the first element of the array of 20 doubles

    //array_type = p ; // *** error *** an array can't be assigned to
    p = a ; // ok; a is implicitly converted to a pointer to the first element
    //static_cast<array_type>(p) ; // *** error *** there is no conversion in the
    // reverse direction; not even explicitly, via a cast

    // the subscript operator is a binary operator between a pointer and an integral type.
    p[5] == 5[p] == *(p+5) == *(5+p) ;

    // this too is ok; because a is implicitly converted to a pointer on which
    // the subscript operator and other pointer operators are applied
    a[5] == 5[a] == *(a+5) == *(5+a);


    // these are equivalent, p is a pointer to the first element of a
    // a is mplicitly converted to a pointer to the first element of a
    a[5] == p[5] ; // both yield a reference to element at position 5 in a

    p += 2 ; // p now points to the element at position 2 in a
    // however, this is an error - though a can be converted to a pointer,
    // it is an array, not a pointer
    // a += 2 ; // *** error

    p[5] == a[7] ; // and both yield a reference to element at position 7 in a
    p[5] == (a+2)[5] == *(a+2+5) == *(a+7) ;

    p[-1] == -1[p] == *(p-1) == *(a+2-1) == *(a+1) == a[1] ;

}


Hi

Pointer, as it says it selfs, is a pointer, it points to an address in memory. It can point to any thing, even to himself :- ) It can point to an array, to an object, to a class and so on...

Array are arrays, either they are allocated statically or dynamically. You can think of an array as a set of memory address, as a set of pointers point to differents address in memory.

As Disch said

The point your professor is trying to get across is that arrays and pointers can be used with the same syntax:


you can access both of them with operator[]
Array are arrays, either they are allocated statically or dynamically. You can think of an array as a set of memory address, as a set of pointers point to differents address in memory.


Careful there. Arrays cannot be allocated dynamically without the user of pointers.
@therockon7therow

Pointer, as it says it selfs, is a pointer, it points to an address in memory. It can point to any thing, even to himself :- )


A pointer can't point to itself if properly used. Imagine this:
1
2
int *pint; // Create a pointer
pint = &pint; // Have pointer point to itself 

This will (should?) throw compiler errors, because &pint is of type **int and you're assigning it to an *int. Technically, I suppose you could cast it to fit:
1
2
int *pint;
pint = (int*)&pint;

But that's a whole lot of messing around to willfully shoot yourself in the foot.
1
2
void* vptr;
vptr = &vptr;
Odd. Why are they an exception? And when would that ever be useful?
Because void pointers can point to anything. It's a leftover from C.

Pointer can point to every thing, even to himself :-) Example void pointer :-) I Love them
@iHutch

Careful there. Arrays cannot be allocated dynamically without the user of pointers.


Again the same statement

Array are arrays, either they are allocated statically or dynamically. You can think of an array as a set of memory address, as a set of pointers point to differents address in memory.
Last edited on

Because void pointers can point to anything. It's a leftover from C.


I love void pointers.

"There's the void. A void it."

Seriously, I have no problem with void pointers. I simply cast them as I need.
Clarification: A void pointer is an object pointer and can point to any type of object.

BUT, it is implementation-defined if a void pointer can point to a function or not.
In the world of C embedded compilers, it is possible for data and code to reside in different address spaces, and even somewhat common to have something like 64KBytes of data (where a 16-bit pointer can be used) but 128KBytes of code (where a 32-bit pointer is required).
> A void pointer is an object pointer and can point to any type of object.

To be precise, a pointer to const volatile void can point to any type of object.

1
2
3
const volatile void* pv1 = nullptr ; // pv1 can point to any object
void* pv2 = nullptr ; // pv2 can be point to any non-const non-volatile object
const void* pv3 = nullptr ; // pv3 can be point to any non-volatile object 


None of them can point to functions or members.
Topic archived. No new replies allowed.