trouble outputing the address location of a character

J

jdcrief

Using Visual C++2005 Expression Edition

Having trouble outputting the value referenced by the pointer and then
outputing the address of the pointer for the char. Any ideas would be
greatly appreciated!



#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
const char* val_4 = "apple juice";

//Right now, pval contains no particular val.

int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char pval_4 = val_4[6];

//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}
 
Z

zeppe

jdcrief said:
Using Visual C++2005 Expression Edition

Having trouble outputting the value referenced by the pointer and then
outputing the address of the pointer for the char. Any ideas would be
greatly appreciated!


//Right now, pval contains no particular val.

int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char pval_4 = val_4[6];

at first, pval_4 it's not a pointer!
cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

secondly, you are unlucky: operator<< for char* is overriden, so you
have to cast to another type of pointer in rder to display it correctly,
like (void*).

Bye,

Zeppe
 
V

Victor Bazarov

jdcrief said:
[..]
//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}

When outputting the addresses cast them all to (void*). The problem
with 'char const*' is that the output operator (the <<) treats it
differently than many other pointers. It tries to output the C string
behind that pointer. If you cast it to void*, the stream will have no
preconceived notion of what the address is of, it will simply output
the pointer value (in hex, probably).

V
 
J

jdcrief

i've decided to go in a different direction....i am stil stuck on
producing the correct output for pvalue. am i close?

#include <iostream>


using std::cout;
using std::cin;
using std::endl;


int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
char value[] = { 'A', '\0' };

//Right now, pval contains no particular val.
int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char* pvalue = value;


//Now, val in this c++ program contains the memory address of the
variable pval
cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;
cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is - "<<pval_3<<endl;

cout<<"Address of the Char value "<<value<<" is - "<<*pvalue<<endl;


return (0);
}



Victor said:
jdcrief said:
[..]
//Now, val in this c++ program contains the memory address of the
variable pval

cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;

cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is- "<<pval_3<<endl;

cout<<"Address of the Char value '"<<val_4<<"' is - "<<pval_4<<endl;

return (0);
}

When outputting the addresses cast them all to (void*). The problem
with 'char const*' is that the output operator (the <<) treats it
differently than many other pointers. It tries to output the C string
behind that pointer. If you cast it to void*, the stream will have no
preconceived notion of what the address is of, it will simply output
the pointer value (in hex, probably).

V
 
T

Thomas Tutone

jdcrief said:
i've decided to go in a different direction....i am stil stuck on
producing the correct output for pvalue. am i close?

#include <iostream>


using std::cout;
using std::cin;
using std::endl;


int main()
{
int val_1 = 10;
long val_2 = 15;
float val_3= 2.86;
char value[] = { 'A', '\0' };

//Right now, pval contains no particular val.
int* pval_1 = &val_1;
long* pval_2 = &val_2;
float* pval_3 = &val_3;
char* pvalue = value;


//Now, val in this c++ program contains the memory address of the
variable pval
cout<<"Address of the Int value "<<val_1<<" is - "<<pval_1<<endl;
cout<<"Address of the Long value "<<val_2<<" is - "<<pval_2<<endl;

cout<<"Address of the Float value "<<val_3<<" is - "<<pval_3<<endl;

cout<<"Address of the Char value "<<value<<" is - "<<*pvalue<<endl;

Victor Bazarov already answered your question, but you ignored him.
Change the above line to the following:

cout << "Address of the char array value "
<< value
<< " is: "
<< static_cast<void*>(pvalue)
<< endl;

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top