Need help figuring out C++ Pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Need help figuring out C++ Pointers

 
0
  #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;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

 
0
  #2
Aug 27th, 2004
Behavior is undefined since ptr2 does not point anywhere.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

 
0
  #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 Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

 
0
  #4
Aug 27th, 2004
Did you try compiling it and running it?
s s
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

 
0
  #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 Quick reply to this message  
Join Date: Aug 2004
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

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

Re: Need help figuring out C++ Pointers

 
0
  #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 Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

 
0
  #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?
???
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 18
Reputation: hfick is an unknown quantity at this point 
Solved Threads: 0
hfick hfick is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

 
0
  #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 Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help figuring out C++ Pointers

 
0
  #10
Aug 27th, 2004
var1 is a double
ptr1 is a pointer to double
...you should be able to handle it from here.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC