Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > output a string's address

Reply
Thread Tools

output a string's address

 
 
ting
Guest
Posts: n/a
 
      10-22-2005
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2005
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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-22-2005

"ting" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
ting
Guest
Posts: n/a
 
      10-24-2005
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;

 
Reply With Quote
 
ting
Guest
Posts: n/a
 
      10-24-2005
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.

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-24-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-24-2005
* 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?
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-24-2005

"ting" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
ting
Guest
Posts: n/a
 
      10-25-2005
certainly. i know that.

 
Reply With Quote
 
ting
Guest
Posts: n/a
 
      10-25-2005
thank anyone who concerns the topic. i knew the result.
i mis-calculated the address. sorry!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments