1

I am trying to understand how pointers work but I do not know how a pointer to only the first element can be used to access all the array

int myArray[10];
for(int i=0; i<10; i++)
{
    myArray[i] = 11*i;
}

int *p;
p = myArray;

//Now how do I access the complete array using the variable p

cout<<*p; //This only prints the first value, how to print all the values
Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • 1
    This might shed some light on how to do it and why it works: http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a – chris Apr 08 '13 at 19:26
  • 1
    `p = myArray` performs *array-to-pointer* conversion on `myArray`. This results in a pointer to the first element. If you want a pointer to the whole array, you have to do `int (*p)[10] = &myArray;`, but you cannot print out a full array with `cout` like this. – Joseph Mansfield Apr 08 '13 at 19:30

6 Answers6

3

You have to use while or for.

int i = 0;
while (i < 10)
  {
    cout << p[i];
    i += 1;
  }

Pointers and arrays are working in the same way. An array is nothing else than a pointer to the first element you allocated.

Julien Fouilhé
  • 2,583
  • 3
  • 30
  • 56
2

except for thr declaration, arrays and pointers con be used using the same syntax (They are different in memory, meaning they still need to be treated differently)

tiridactil
  • 389
  • 1
  • 11
2

Use like this,

int *p;
p = myArray;
for(int i=0;i<10;i++)
{
    cout<<*(p+i);
}
shivakumar
  • 3,297
  • 19
  • 28
2

If you for example want to access pos 5, you can just write:

...
int *p;
p = myArray;

cout << p[5];

Since the compiler know that p is a pointer to an int, it will add the size of an int for each step (4 bytes in this case). As long as you don't use pointers to void, the compiler does this for you. You still have to keep track of the length of the array so you do not exceeds it since a pointer don't do that.

Floaf
  • 145
  • 7
  • What do you mean when you say `it will add the size of an int for each step (4 bytes in this case)`. Do pointers hold the size too, I thought they just held the location like 00fx09 something? Sorry I am a newb. – Joe Slater Apr 08 '13 at 19:38
  • Since ints are 4 byte, the compiler have to multiply the position you want to access by 4 (if you want to access pos 5 in the array, it vill access it by taking the address p points to and add 5*4=20 bytes to it. And no, pointers does not contain any size, but it is declared as pointer to a type (int in this case) and thats why the compiler knows the size of each element in the array. It knows it's 4 bytes because you defined p as int *p; – Floaf Apr 08 '13 at 19:50
1

The first element points to the first memory location of the elements in the array. So this:

myArray[0];

and

myArray;

point to the same location. You can use indexes on the pointer, just like you did to fill the array. So this:

int *p = myArray;
cout << p[0];
cout << p[1];

would access your other elements. You can use a for loop to access all the elements in the array, just like you did to populate it in the first place.

Frecklefoot
  • 1,660
  • 2
  • 21
  • 52
1

You can think of the name of an array as a pointer to its first element. So, the line p = myArray; is simply copying the address of the first element of the array myArray into p.

Now the line cout<<*p; is obviously displaying the value of what's pointed by p, which is the first element of your array.

To display all the elements, you can simply use a for loop like you did before.