5

In attempt to explain that arrays are just pointers (in C++) to our class, my professor showed us this:

array[5]      // cout'ing this 
*(array + 5)  // would return the same value as this

I'm having a little trouble completely understanding it. Here's my thinking:

array is the address of the first location and so if we add 5 to that address, we move 5 addresses in memory. The pointer operator pulls the data from the memory location.

Is this the correct idea? The idea still feels foggy with me and just feel like I don't understand it completely. I think hearing someone else explain it might help me understand it more. Thanks in advance!

Austin Moore
  • 1,414
  • 6
  • 20
  • 43
  • 2
    The first thing (array indexing) is a shorthand for the second (explicit pointer arithmetic). The second one moves a pointer 5 * `sizeof(underlying type of pointer or array)`, but it's still some address, so you need to `*` to dereference and get the real value. – wkl Nov 21 '11 at 04:37
  • @Dani Sorry if the 'question' is kind of unclear. I realize the concept, but it's fuzzy in my mind and I was hoping that having someone else explain it would clear it up for me. – Austin Moore Nov 21 '11 at 04:51
  • 2
    "In attempt to explain that arrays are just pointers (in C++) to our class, my professor..." ...ought to read a book. Arrays are not pointers. Please disregard your professor's most recent lecture (except to pass a test). – GManNickG Nov 21 '11 at 05:02
  • [relevant FAQ](http://stackoverflow.com/questions/4810664/) – fredoverflow Nov 21 '11 at 07:19

4 Answers4

4

You've got the right idea.

Arrays implicitly cast to pointers. Interestingly, [] works on pointers, not arrays.

a[b] Subscript operator is defined as *(a + (b)). [] is used as syntactic sugar - it's much more pleasant to write array[5] instead of *(array + 5)

Pointer arithmetic with a pointer and an integer p + i increases the address at p by i * sizeof(*p) bytes.

char* p; 
p + 5; // address increased by 5

int* p;
p + 5; // address increased by 20 as sizeof(*p) is 4

The * operator performs indirection. It will give you what the pointer is pointing to.

int x[2];
int* p = &x[0]; // could also be int* p = x;
*p = 5;         // x[0] is now 5
*(p + 1) = 10;  // x[1] is now 10
Pubby
  • 51,882
  • 13
  • 139
  • 180
  • 1
    This is a good answer, except that `[]` is more than just syntactic sugar. In C, this statement would be valid, but in C++ `[]` is so much more than that, thanks to operator overloading. In C++, the index operator is defined for pointers of builtin types to be what you stated, but user defined types are free to define it as they see fit. – Michael Price Nov 21 '11 at 05:07
  • @MichaelPrice Good point. Although `[]` is for all pointer types, while overloading it only works on classes/unions. – Pubby Nov 21 '11 at 05:23
4

Your professor is correct that the two expressions will produce the same results. Your explanation of it leads me to believe you have a good grasp of the mechanics.

It is not quite correct to say an array is the same as a pointer. An array is very easily converted to a pointer, which leads to some confusion.

For example consider this code:

int array[5];
int * pointer = new int[5];
cout << sizeof(array);   // outputs 5*sizeof(int), most probably 20
cout << sizeof(pointer); // outputs sizeof(int*), most probably 4 on a 32 bit OS
array[4] = 906;
pointer[4] = 906;
cout << *(array + 4) << *(pointer + 4); // outputs "906 906"
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

from memory arrays in c++ are sections of continuous memory the program. thats why you can go *(array + 5) as it is 5 from the front of the array.

Also keep in mind that arrays in C++ start @ 0 so thus the 6th element in an array is array[5] example

[0] [1] [2] [3] [4] [5] Array index
 1   2   3   4   5   6  Item number

To access item 2 u would either

  • array[1]
  • *(array+1)

but please keep in mind array[index_number] is standard syntax for looking up an item in an array so make sure u use array[index_num] instead of *(array + index_num)

CStreel
  • 2,642
  • 2
  • 19
  • 37
2

Yes, what your proffessor said is correct.

to explain it in detail

Consider your array,

int array[5]

here it takes 5 location in the memory say 0x00 0x04 0x08 0x0C and 0x10 0x14

Assuming integer takes 4 bytes, the locations are 4 bytes apart.

it this case 'array' represents the base address (pointer) to the array, that is 0x00. And the type of a would be int *. (since the type of the array elements is integer)

If you do array+1, it will be 0x04, since the increment of the pointer depends on the type of the pointer. If the pointer is integer, it will increment by 4 bytes. If the type of the integer is character, the pointer would increment by one bytes.

so if you do (array+5) it would point to the address 0x14 and

*(array+5)

returns the value of the location 0x14 which is the value at 5th location of array.

so in practical, array[5] is equal to *(array+5)

The compiler internally converts array[5] to *(array+5)

so even if we write 5[array], it will get converted it to *(5+array)

Though it seems strange, this is the reason why 5[array] works same as array[5]

M S
  • 3,995
  • 3
  • 25
  • 36