By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

output a string's address

P: n/a
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.

Oct 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
ting wrote:
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
Oct 22 '05 #2

P: n/a

"ting" <ti*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Oct 22 '05 #3

P: n/a
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;

Oct 24 '05 #4

P: n/a
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.

Oct 24 '05 #5

P: n/a
ting wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 24 '05 #6

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 24 '05 #7

P: n/a

"ting" <ti*******@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Oct 24 '05 #8

P: n/a
certainly. i know that.

Oct 25 '05 #9

P: n/a
thank anyone who concerns the topic. i knew the result.
i mis-calculated the address. sorry!

Oct 25 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.