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
2
  • 1
    This might shed some light on how to do it and why it works: stackoverflow.com/questions/381542/… Commented Apr 8, 2013 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. Commented Apr 8, 2013 at 19:30

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
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;
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)

1 Comment

@tiradactil Either you edited quickly or I misread your post!
2

Use like this,

int *p;
p = myArray;
for(int i=0;i<10;i++)
{
    cout<<*(p+i);
}

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.