943,460 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5278
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

:o sorry, just now i refer to the first code that you given. The right answer is s s.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hail2dthief is offline Offline
10 posts
since Aug 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

&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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hfick is offline Offline
18 posts
since Aug 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

Here's a link:Check it and see, ask if in doubt

http://www.daniweb.com/techtalkforums/thread9878.html
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 27th, 2004
0

Re: Need help figuring out C++ Pointers

Quote 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';
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manoj9_5 is offline Offline
2 posts
since Oct 2004
Oct 27th, 2004
0

Re: Need help figuring out C++ Pointers

Quote 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error in c++ program
Next Thread in C++ Forum Timeline: database access using c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC