output a string's address

Discussion in 'C++' started by ting, Oct 22, 2005.

  1. ting

    ting Guest

    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.
    ting, Oct 22, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Oct 22, 2005
    #2
    1. Advertising

  3. ting

    Mike Wahler Guest

    "ting" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 22, 2005
    #3
  4. ting

    ting Guest

    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;
    ting, Oct 24, 2005
    #4
  5. ting

    ting Guest

    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.
    ting, Oct 24, 2005
    #5
  6. 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
    Karl Heinz Buchegger, Oct 24, 2005
    #6
  7. * 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?
    Alf P. Steinbach, Oct 24, 2005
    #7
  8. ting

    Mike Wahler Guest

    "ting" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 24, 2005
    #8
  9. ting

    ting Guest

    certainly. i know that.
    ting, Oct 25, 2005
    #9
  10. ting

    ting Guest

    thank anyone who concerns the topic. i knew the result.
    i mis-calculated the address. sorry!
    ting, Oct 25, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,222
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    883
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    435
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    778
    Alex Stevens
    Aug 10, 2004
  5. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    534
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page