Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,119 people online right now. Registration is fast and FREE... Join Now!




WHAT IS THE DIFFRENCE between &(ptarray)[i] and (ptarray[i])

 
Reply to this topicStart new topic

WHAT IS THE DIFFRENCE between &(ptarray)[i] and (ptarray[i])

virendra
20 Sep, 2006 - 02:54 AM
Post #1

New D.I.C Head
*

Joined: 12 Sep, 2006
Posts: 14


My Contributions
CODE

# 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
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: WHAT IS THE DIFFRENCE Between &(ptarray)[i] And (ptarray[i])
23 Sep, 2006 - 08:00 AM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
simple:
&(ptarray)[i] is the address of da ith element of the array of pointers :ptarray.
(ptarray[i]) is the address of da ith element of the array of integers: arr

the wierd values ure getting are memory addresses. Check da C++ tutorials section. There's a gr8 tutorial called "Pointers".
smile.gif
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: WHAT IS THE DIFFRENCE Between &(ptarray)[i] And (ptarray[i])
23 Sep, 2006 - 04:27 PM
Post #3

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
&(ptarray)[i] will be the address of the i'th element of ptarray.

(ptarray[i]) will be the actual value contained in the i'th element of ptarray.

the reason for this is because the [ ]'s will automatically de-reference the element.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month