954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help figuring out C++ Pointers

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;

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

Behavior is undefined since ptr2 does not point anywhere.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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;

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

Did you try compiling it and running it?s s

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

hail2dthief
Newbie Poster
10 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

no..because i don't understand how it works..can you please explain it to me...

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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?

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 
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?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????

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

var1 is a double
ptr1 is a pointer to double
...you should be able to handle it from here.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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;

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

:o sorry, just now i refer to the first code that you given. The right answer is s s.

hail2dthief
Newbie Poster
10 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 
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?

hfick
Newbie Poster
18 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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

http://www.daniweb.com/techtalkforums/thread9878.html

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

manoj9_5
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
----------------------------------------------------------- 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You