output a string's address

T

ting

I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";
char *p;
p=str;
//output "ABCD" address.why is (long)p different from (void *)p ?
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.
 
V

Victor Bazarov

ting said:
I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";

This is deprecated. Initialising a pointer to non-const char with
a literal creates a dangerous illusion that you are allowed to change
the contents of the memory. It's a C-ism. You should start to drop
those habits.
char *p;
p=str;

This is not even a C-ism. Why declare and then assign when you can
simply initialise

char *p = str;

?
//output "ABCD" address.why is (long)p different from (void *)p ?

How is it different? One is output as decimal and the other as hex?
That's the way cout outputs numbers versus pointers.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

The C-style casts are another thing you should abandon.
return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.

Well, next time tell us _how_ it is different, and we will make a guess
why that might be.

V
 
M

Mike Wahler

ting said:
I'm a newer in C++.
I hope to output a string's address. The program is as follows:

#include <iostream>
using namespace std;

int main()
{
char *str="ABCD";
char *p;
p=str;
//output "ABCD" address.why is (long)p different from (void *)p ?
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

return 0;

}

I don't understand why (long)p is different from (void *)p . please
tell me. thanks in advance.

A pointer is not an integer. An integer is not a pointer.
Why do you believe differently?

-Mike
 
T

ting

thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;
 
T

ting

thank you anyway.i think the two ways can output a pointer's address, i
only concerns why they are different when changed to decimal or hex.
 
K

Karl Heinz Buchegger

ting said:
thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

Can you show what output you get, or is this a secret?
 
A

Alf P. Steinbach

* ting:
thank you anyway. i knew "One is output as decimal and the other as
hex".but the
two values are different when all changed to decimal or hex,which is my
concern.
cout<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

The values are not different, but their presentation might be.

Try

cout<<hex<<"(long)p = "<<(long)p<<endl;
cout<<"(void *)p = "<<(void *)p<<endl;

And don't respond "still different": as a minimum, include your output, what
you think is different, and info about your compiler.
 
M

Mike Wahler

ting said:
thank you anyway.i think the two ways can output a pointer's address, i
only concerns why they are different when changed to decimal or hex.

Are you aware that, for example, the
values 0x0A and 10 (decimal) are *not*
different, but exactly equal?

-Mike
 
T

ting

thank anyone who concerns the topic. i knew the result.
i mis-calculated the address. sorry!
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top