0

I saw this code in this link-http://www.tutorialspoint.com/cplusplus/cpp_pointers_vs_arrays.htm. Look at the first piece of code.

#include <iostream>

using namespace std;
const int MAX = 3;

int main ()
{
int  var[MAX] = {10, 100, 200};
int  *ptr;

// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
   cout << "Address of var[" << i << "] = ";
   cout << ptr << endl;

   cout << "Value of var[" << i << "] = ";
   cout << *ptr << endl;

   // point to the next location
   ptr++;
}
return 0;
}

Shouldn't it be ptr = &var instead of ptr = var? It is below the comment. Why is it declared simply asvar instead of &var?

Siddharth Venu
  • 1,318
  • 13
  • 26

2 Answers2

8

var will decay into a pointer to its first element.

ptr = var;

is equivalent to

ptr = &var[0];

&var is a pointer to an array, not to an int.

int (*aptr)[MAX] = &var;

would be valid, but it doesn't mean the same - *aptr is an array with MAX elements, not an int.

The tutorial's claim that "pointers and arrays are interchangeable in many cases" is completely wrong.

The only time you can "interchange" anything is this case; when something expects a pointer, an array decays into a pointer to its first element.
There is not a single case where you can use a pointer in place of an array.

Confusingly, ptr is often called "a pointer to an array" or even "an array" in informal conversation, even though this is formally incorrect.
This is because "a pointer to the first element of an array" is quite a mouthful, and to a non-beginner it's usually clear from context what is actually meant.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you. I now understand why that syntax is correct. – Siddharth Venu Feb 10 '16 at 09:35
  • "There is not a single case where you can use a pointer in place of an array" is arguably "more wrong" than "they are completely exchangeable". For things like parameter passing, use in pointer arithmetic expressions and indexing, which are easily the most common operations both with arrays and pointers, they are indeed completely interchangeable – Peter - Reinstate Monica Feb 10 '16 at 09:46
  • @PeterA.Schneider More examples of array->pointer decay. "Interchangeable" is bidirectional. – molbdnilo Feb 10 '16 at 09:58
2

An array "decays" to a pointer to its first element in assignments, parameter passing etc. That would be a pointer to int, which is what p is declared as.

You actually can take the address of the array, although it is much less common. The address then is the address of the whole array, like this:

int (*parr)[3] = &var;

Now cou can say, for example, int i = (*parr)[1]; to initialize i with the second element of var, 100.

The address of the whole array is numerically identical with the address of its first element because the first element is where the array starts, after all. That may seem funny: What's the point then?

The difference is in type, which (apart from mere grammar questions about type compatibility -- but you can always cast those away in C!) determines what happens when you do pointer arithmetics. Consider

int *ptr = var;
int (*parr)[3] = &var;

printf("ptr: %p, parr: %p\n", ptr, parr);
printf("Increment ptr: %p, incremented parr: %p\n", ptr+1, parr+1);
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62