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

Re: Need help figuring out C++ Pointers

 
0
  #11
Aug 27th, 2004
ok i ran this code now and i get s s not x x...are you sure x x is right????
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: 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
  #12
Aug 27th, 2004
:o sorry, just now i refer to the first code that you given. The right answer is s s.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Need help figuring out C++ Pointers

 
0
  #13
Aug 27th, 2004
&val means address of the varialble var
*p (if is a pointer (int *p) means value at address pointed at by p.Changes this will change the variable it points to.

int val = 1;
p=&val;
val+=10

cout<<val<<" *p="<<*p; //output is the same for both
(*p) += 4; //changes the value of val
cout<<val<<" *p="<<*p; //output is the same for both

The outputs are:
11 11
15 15
See what you can, remember what you need

Fourzon | Earn via Coding
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
  #14
Aug 27th, 2004
Originally Posted by hfick
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
so what would the data type of ptr1 be? would it be just ptr1 since it doesn't have the (*) in front of it or how does that work?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Need help figuring out C++ Pointers

 
0
  #15
Aug 27th, 2004
Here's a link:Check it and see, ask if in doubt

http://www.daniweb.com/techtalkforums/thread9878.html
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,414
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: 248
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
  #16
Aug 27th, 2004
Originally Posted by hfick
so what would the data type of ptr1 be? would it be just ptr1 since it doesn't have the (*) in front of it or how does that work?
Uh, look up a couple posts where I answered that.
"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: Oct 2004
Posts: 2
Reputation: manoj9_5 is an unknown quantity at this point 
Solved Threads: 0
manoj9_5 manoj9_5 is offline Offline
Newbie Poster

Re: Need help figuring out C++ Pointers

 
0
  #17
Oct 27th, 2004
Originally Posted by hfick
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;
-----------------------------------------------------------
output will be
X X
because ptr1 point to the var2 and var2 contain value 'x';
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help figuring out C++ Pointers

 
0
  #18
Oct 27th, 2004
Originally Posted by manoj9_5
-----------------------------------------------------------
output will be
X X
because ptr1 point to the var2 and var2 contain value 'x';
Not necessarily. At that point dereferencing an uninitialized pointer has caused undefined behavior. After that, nothing is guaranteed to work, even if it would otherwise.
I'm here to prove you wrong.
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