| | |
what is the difference between (ptarray[]) AND &(ptarray[i]) in below example
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
# 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
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
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
So
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)
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Re: what is the difference between (ptarray[]) AND &(ptarray[i]) in below example
1
#3 Sep 20th, 2006
•
•
•
•
what is the difference between &ptarray[i] &
ptarray[i] due to that the address location of output 2 & output 3 is changing .
I hope it helps.
Last edited by Micko; Sep 20th, 2006 at 9:35 am.
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)
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.
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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)
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?
Look at this code (modified loop)
C++ Syntax (Toggle Plain Text)
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?
Last edited by Micko; Sep 21st, 2006 at 3:49 am.
![]() |
Similar Threads
- What is the difference between simple form & MDI form? (Visual Basic 4 / 5 / 6)
- Difference between asp & asp.net (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: Does anyone have a "CLUE"?
- Next Thread: HELP needed in VC++ Project!!!
Views: 1124 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





