# Array of Pointers Pointing to an unknown location

• A+
Category：Languages

I was practicing with array of pointers. i wrote a simple code.

``#include<iostream> using namespace std;  int main(){      int a[3]={1,2,3};     int *b[3];      for(int i=0; i<3; i++){         b[i] = &a[i];     }      cout<<b; //This gives a confusing output     cout<<endl;      for(int i=0; i<3; i++){         cout<<b[i];         cout<<endl;     } } ``

and Output is

``0x6ffe10 0x6ffe30 0x6ffe34 0x6ffe38 ``

Now I don't understand where the b pointing. I know this sounds weird but i wanna know. where the b pointer pointing. The b[0], b[1], b[2] gives expected result.

`b` is an array of pointers to `int`. It's address is the address of the first pointer.

• The first pointer `b[0]` is assigned the address of element `a[0]` (so `b[0]` points to `1`).
• `b[1]` is assigned the address of `a[1]` (so `b[1]` points to `2`)
• `b[2]` is assigned the address of `a[2]` (so `b[2]` points to `3`)

In each case the pointer in the `b` array holds the address of the corresponding element within `a`. So if there are any changes made to the elements of `a` after your loop assigning the address in `a` to `b`, the value held in the memory location pointed to by the element of `b` will change, but the address for that value held by `b` will remain unchanged.