943,521 Members | Top Members by Rank

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

Need help figuring out C++ Pointers

Expand Post »
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;
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

Behavior is undefined since ptr2 does not point anywhere.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

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;
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

Did you try compiling it and running it?
Quote ...
s s
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 27th, 2004
0

Re: Need help figuring out C++ Pointers

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.
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

no..because i don't understand how it works..can you please explain it to me...
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

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?
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

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

Re: Need help figuring out C++ Pointers

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
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

var1 is a double
ptr1 is a pointer to double
...you should be able to handle it from here.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 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