# include<iostream.h>
int main()
int ar[2]={8,2}; // (1)

int var1=66; //(2)

int var2=111; //(3)

int* ptarray[5]; //(4)

ptarray[0]= ar; //(5) 

ptarray[1]= &ar[1]; //Address of second element of array ar // (5)

ptarray[2]= &var1; //(6)

ptarray[3]= &var2; //(7)

ptarray[4]= &ar[0]; //(8)

// To keep code small I use a loop //(9)

for(int i=0;i<5;i++)

cout<<*(ptarray[i])<<endl; // (10)



OUTPUT 1:     8

CASE 2: IF i replace *(ptarray[i]) with    &(ptarray[i])  in LINE 

NO 10 IN COUT <<  statement then output is 

OUTPUT 2 :    0x 8fb8ffe4 
                    0x 8fb8ffe6 
                     0x 8fb8ffe8 
                    0x 8fb8ffea 
                    0x 8fb8ffec

CASE 3: IF I REPLACE *(ptarray[i]) with (ptarray[i]) in LINE 

question :

what is the difference between &ptarray[i] & 
ptarray[i] due to that the address location of output 2 & output 3 is changing .

thanx in advaNCE

Edited by pyTony: fixed formatting

10 Years
Discussion Span
Last Post by Micko

You should go throught pointer tutorial.
OK *(ptarray[i]) is dereferenced pointer of ptarray[i] . Here is the deal. ptarray[0] value is address of ar . When you dereference it you will have the first element of the ar array which is 8. It is very similar to ptarray[4] becouse it's value is the address of the first element of ar array. Thats why *(ptarray[0]) and *(ptarray[4]) is equal as same as ptarray[0] and ptarray[4] . &ptarray[i] is the addreses of ptrarray[i] .
So int* ptarray[5]; is array of pointers.


what is the difference between &ptarray &
ptarray due to that the address location of output 2 & output 3 is changing .

You defined an array of pointers to int named ptarray. Array's elements are pointers. So ptarray represent array element which is actually address of some variable elswhere. For example, if address of variable "var" is placed to ptrarray as i+1 element, ptarray referes to address of variable "var". On the other hand, &ptarray is memory address of ptarray. Arrays are also placed in memory and each array's elements has it's own address. So, &ptarray represents memory address of array's element, and it's value (ptarray) represents memory address of variable "var" because ptarray is an array of pointers. Remember, pointer is nothing more than variable that store memory address of some other variable.
I hope it helps.


ok ,so ptarray the is the pointer array which is holidng the address of some variable and &(ptarray) is the derefrence i.e. it holds the address of ptarray location i.e.pointer to pointer
Is I am right if not please clear my doubt
and so, why the last digit of output 2 & 3 changes i.e.
For integer the adress changes with two bytes as in output 2 ok it is underastandable but in output 3 why the last digit of output address differ with more than two bytes i.e. 2 ,4 ,0,e(last digit of out put 3)
really thanks for these two reply

virendra :?:
(intermediate programmer)


I assume your compiler is rather old.
Look at this code (modified loop)

for(int i=0;i<5;i++)
    out<<&(ptarray[i])<<"\t"<<ptarray[i]<<endl; // (10)

And this is my output:

0x22ff30 0x22ff58
0x22ff34 0x22ff5c
0x22ff38 0x22ff54
0x22ff3c 0x22ff50
0x22ff40 0x22ff58

This makes sense. First column represents addresses of array elements. Since arrays are continuos in memory (on most compilers I work with, althought I'm not sure if this is guaranteed by the Standard) and on most 32 bit machines size(void*) is 4 bytes (this is also not guaranteed) first column rows are evenly spaced by 4 bytes.
Second column is a different story. First two rows represents addresses of array elements and difference is 4 byte. Also, last element of array hold address of first element of "ar" (same as ptarray[0]), so that's ok.
Other elements hold addresses of variables var1 and var2. There is no guarantee they will be evenly spaced, and probably in most cases they won't.
Is this clear to you?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.