what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 4
Reputation: yadavvirendra is an unknown quantity at this point 
Solved Threads: 1
yadavvirendra's Avatar
yadavvirendra yadavvirendra is offline Offline
Newbie Poster

what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

 
1
  #1
Sep 20th, 2006
# 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)
}


CASE1:

OUTPUT 1: 8
2
6
11
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
NO 10 THE OUT PUT IS
OUTPUT3:
0x8fb8fff2
0x8fb8fff4
0x8fb8fff0
0x8fb8fffe
0x8fb8fff2
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
virendra
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

 
1
  #2
Sep 20th, 2006
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.
Last edited by andor; Sep 20th, 2006 at 9:12 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

 
1
  #3
Sep 20th, 2006
Originally Posted by yadavvirendra View Post
what is the difference between &ptarray[i] &
ptarray[i] 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[i] 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[i] referes to address of variable "var". On the other hand, &ptarray[i] is memory address of ptarray[i]. Arrays are also placed in memory and each array's elements has it's own address. So, &ptarray[i] represents memory address of array's element, and it's value (ptarray[i]) 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.
Last edited by Micko; Sep 20th, 2006 at 9:35 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: yadavvirendra is an unknown quantity at this point 
Solved Threads: 1
yadavvirendra's Avatar
yadavvirendra yadavvirendra is offline Offline
Newbie Poster

Re: what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

 
1
  #4
Sep 21st, 2006
ok ,so ptarray[i] the is the pointer array which is holidng the address of some variable and &(ptarray[i]) is the derefrence i.e. it holds the address of ptarray[i] 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)
Last edited by WolfPack; Sep 21st, 2006 at 3:16 am. Reason: All Capital letters is considered rude. Changed Uppercase to Sentence case.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: what is the difference between (ptarray[]) AND &(ptarray[i]) in below example

 
0
  #5
Sep 21st, 2006
I assume your compiler is rather old.
Look at this code (modified loop)
  1. for(int i=0;i<5;i++)
  2. 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?
Last edited by Micko; Sep 21st, 2006 at 3:49 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1124 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC