Arrays and Pointers

Started by
8 comments, last by Mxz 18 years ago
I know it's being said that arrays are not equal to pointers and every good programmer would know that. I guess I am not a good programmer. So please enlighten me by telling the difference between them.
Advertisement
int* a;int b[5];cout << sizeof(a) << endl;cout << sizeof(b) << endl;cout << typeid(a).name() << endl;cout << typeid(b).name() << endl;


CM
So basically both are pointers.This is what you want to say??
Quote:Original post by TEUTON
So basically both are pointers.This is what you want to say??

I'll bet if you ran the code, you'd come to different conclusions.

Actually, I suppose the second two outputs might work against me if not ran on VS. Definately pay attention to the first two, though, as they should be insightful.

CM
In VC6.0
Output

4
20
int *
int *

In DEV C++
Output

4
20
Pi
A5_i

So according to VC both are pointers.Isn't it?
I'm about to go to bed, so I'll just up and say what that code was meant to get across: one important difference is that they're different types. Static arrays have size information tacked on as well.

There's also the fact that pointers can be reseated [ int* x = 0; x = &y x = &z x = 0;], while arrays can not.

CM
Quote:Original post by TEUTON
So according to VC both are pointers.Isn't it?

If they are both pointers, how come one is five times larger than the other?

CM
Quote:Original post by Conner McCloud
Quote:Original post by TEUTON
So according to VC both are pointers.Isn't it?

If they are both pointers, how come one is five times larger than the other?
CM


The ancestors of the c language didn't have arrays, just pointers, and all array operations were done with pointer plus offset pairs dereferenced. The c language simplified this by adding the array operator while still allowing the old form. There are special kind of c constants, called static arrays. This is the only kind of array that has a size, every other array is represented as a pointer to a memory area. To make static and dynamic arrays look equal, every static array looks like a pointer when referenced, but because they are constant, you can't change them. The static array is equal to the constant number. You can't use constants as left values.

Viktor
Sigh.
Check out the comp.lang.c faq.
Quote:Original post by TEUTON
I know it's being said that arrays are not equal to pointers and every good programmer would know that.
I guess I am not a good programmer. So please enlighten me by telling the difference between them.



You are correct in claiming that arrays and pointers are not equal. An array is a contiguous series of elements in memory. A pointer is a variable which stores a memory address.

Arrays and pointers do share some links though.


  • An array can decay into a pointer to the first element

    There is an implicit conversion from an 'array of N elements of type T' to a pointer of type T. This allows for code like the following, which results in the variable parray containing the memory address of the first element of the array of 10 integers.

    int array[10];int* parray = array;


    A more common use of this might be string literals, which are simply arrays of const chars.

    const char* parray = "Hello";


    Here the string literal "Hello" is of type const char [6], and so has an implicit conversion, or decay, into a pointer of type const char*.


  • The subscript operator behaves equally for both arrays and pointers


    When you invoke the [] operator on an array or a pointer, like so
    E1[E2]

    The result is equal to the following statement
    *(E1 + E2)

    This requirement means that E1[E2] results in the same value independent of whether E1 is an array or E1 is a pointer to the first element of the same array. If that is not obvious then perhaps it is clearer when you consider the following fragment again.
    *(E1 + E2)

    The only way this expression can be valid if E1 is an array is if E1 decays to a pointer to the first element through the previously mentioned implicit conversion. In which case, you end up with the same result as if E1 was a pointer to begin with (i.e. *(pointer + E2))

    A slightly less abstract example might be

    int array[10];array[9] = 10; //*(array + 9) = 10;


    Conversely, the above definitions also allow for the commonly known obfuscation whereby the indexing integer is outside the subscript operator, and the array or pointer is within the subscript operator (i.e. E2 is the pointer or array and E1 is the indexing integer or enumeration). This is demonstrated by the following code.

    int array[10];9[array] = 10; //*(9 + array ) = 10;


    These requirements are also what allows for the use of dynamic arrays like the following.

    int* pointer = new int[10];pointer[9] = 10; //*(pointer + 9) = 10;9[pointer] = 10; //*(9 + pointer) = 10;delete [] pointer;


    Although there are these similarities, it is important to note that pointers and arrays are completely different types and concepts in C++. Pointers can be, and often are, used completely outside the domain of arrays.
  • This topic is closed to new replies.

    Advertisement