3
#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;
}

I don't understand why this code should work as the correct syntax is

ptr=&var[0];

instead of

ptr=var;

if var would have been something like

int var=49;

then the my version of reasoning holds and earlier program fails. Is their any distinct concept of pointer for array and single data point?

4 Answers4

5

I don't understand why this code should work as the correct syntax is

ptr=&var[0];

instead of

ptr=var;

The two ways of assigning a pointer are equivalent. In expressions that assign an array variable to a pointer C++ interprets the array name as a pointer to the initial element of the array, i.e. &var[0].

If var would have been something like

int var=49;

then the my version of reasoning holds and earlier program fails.

This is because C++ provides an implicit conversion to pointer from arrays, but not from scalar variables.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

First, we should understand that pointers and arrays are handled the same in C or C++, at least once we've moved past the definition. Let's make sure we understand a few things. From the example you have described above, the following are equivalent (after ptr=var).

var[0], *var, ptr[0], ptr. All == 10.

In the case of:

ptr = &var[0];

That is assigning the address of the first element of var. Another way to say that is that &var is a pointer to a pointer to an int. We know that var is a pointer to an int (int *) and *var is an int.

If *var and var[0] are equivalent, then we're doing the equivalent of *(&var), which is just var.

Maybe to make this more clear, you could think about a different index than 0. var[1] is the same as (var + 1). It holds then that:

ptr = var + 1

is the same as

ptr = &var[1]

They're just two different ways to express the same thing.

In your example of int var=49, var is an int, not a pointer to an int (remember that an array is just a pointer, so pointer to an int is the same as an array of ints). You could express that as:

int var=49;
ptr = &var;

Then if you remove the loop (or set MAX=1) you'll get the result you're looking for.

Think of * as removing a level of addressing (pointers) and & as adding one. It's a little confusing because * does the opposite when you're defining them.

int **i;

Means a pointer to a pointer of an int. Then:

&i - pointer to a pointer to a pointer of an int. i - pointer to a pointer of an int. *i - pointer to an int **i - int i[0] - pointer of an int *i[0] - int

Pointers in C and C++ are simply another datatype. The question of "Is their any distinct concept of pointer for array and single data point?" is a bit flawed because in your example you have an array (which is equivalent to a pointer), not an array to a pointer. A single data point (in your second example int var=49) still has an address, but isn't an array, so it isn't a pointer type, it is an integer type. You could have easily described that as:

int var[1] = {49};

Which is an array (pointer) to a single int, which should not be confused with an int.

geis
  • 306
  • 1
  • 4
-1

An array name holds the address of its first element in memory. i.e a pointer.

int array[2] = {1,2}; // "array" is a pointer to array[0]
int **p = &array;  //pointer to pointer to array[0]
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
-1

The name of array is a reference to first index of array. The correct way of assigning a pointer to array is:

int *ptr = arrayName;

OR

int *ptr = &arrayName[0];

An other thing to keep in mind is that you can add offset to pointer to reference other elements of array. This will point to the first value of aray *ptr++; OR *ptr+=1;. Whereas doing this with variable pointer will yield unexpected results.

Zain Zafar
  • 1,607
  • 4
  • 14
  • 23