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;

Behavior is undefined since ptr2 does not point anywhere.

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;

Did you try compiling it and running it?

s s

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.

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

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?

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?

???

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

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

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;

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

&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

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?

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.