User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,789 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3759 | Replies: 17
Reply
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Solution Need help figuring out C++ Pointers

  #1  
Aug 27th, 2004
I am supposed to figure out what the output produced would be from this equation..can anyone help me and explain it to me

char var1='s';
char var2='x';
char *ptr1, *ptr2;
ptr1=&var2;
*ptr2=*ptr1;
cout << *ptr1 << " " < var2 << endl;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

  #2  
Aug 27th, 2004
Behavior is undefined since ptr2 does not point anywhere.
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Solution Re: Need help figuring out C++ Pointers

  #3  
Aug 27th, 2004
Sorry i missed a line when i typed it in..could you take another look now..

char var1='s';
char var2='x';
char *ptr1, *ptr2;
ptr1=&var1;
ptr2=&var2;
*ptr2=*ptr1;
cout << *ptr1 << " " << var2 << endl;
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

  #4  
Aug 27th, 2004
Did you try compiling it and running it?
s s
Reply With Quote  
Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

  #5  
Aug 27th, 2004
The code you given has some error, are you sure you are giving the right code?
Anyway, the output should be:

x x

cause ptr1 has been assigned to the address of var2. When you output the value of *ptr1, it will refer to the value of that address.
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

  #6  
Aug 27th, 2004
no..because i don't understand how it works..can you please explain it to me...
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

  #7  
Aug 27th, 2004
i need to know the data types of variables such as:

double var1, *ptr1, *ptr2;
float *ptr3;
int var2, *var4....do you know anything about data types?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

  #8  
Aug 27th, 2004
Originally Posted by hail2dthief
The code you given has some error, are you sure you are giving the right code?
Anyway, the output should be:

x x

cause ptr1 has been assigned to the address of var2. When you output the value of *ptr1, it will refer to the value of that address.
What are you talking about?
Originally Posted by hfick
i need to know the data types of variables such as:

double var1, *ptr1, *ptr2;
float *ptr3;
int var2, *var4....do you know anything about data types?
???
Reply With Quote  
Join Date: Aug 2004
Location: Minnesota
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

  #9  
Aug 27th, 2004
the next question asks says i am supposed to state the data type of each variable...
double var1, *ptr1, *ptr2;
float *ptr3;
int var2, *var4
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

  #10  
Aug 27th, 2004
var1 is a double
ptr1 is a pointer to double
...you should be able to handle it from here.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC