Address of a string

Discussion in 'C++' started by ruffiano, Aug 15, 2006.

  1. ruffiano

    ruffiano Guest

    Does the string class have a method for retrieving the address of a
    string?

    Thanks.
    ruffiano, Aug 15, 2006
    #1
    1. Advertising

  2. ruffiano

    Phlip Guest

    ruffiano wrote:

    > Does the string class have a method for retrieving the address of a
    > string?


    You mean std::string::data() and sd::string::c_str()?

    They might not do what you think...

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
    Phlip, Aug 15, 2006
    #2
    1. Advertising

  3. ruffiano wrote:
    > Does the string class have a method for retrieving the address of a
    > string?


    Which string and address of what string? For 'std::string' please RTFM,
    it has 'c_str' and 'data' members you might find useful.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 15, 2006
    #3
  4. ruffiano

    ruffiano Guest

    Hi, I was trying to use the string with memcpy.

    // void *memcpy( void *dest, const void *src, size_t count );

    Thanks again for the quick answers. This ng is really great.



    ruffiano wrote:
    > Does the string class have a method for retrieving the address of a
    > string?
    >
    > Thanks.
    ruffiano, Aug 15, 2006
    #4
  5. ruffiano

    Jim Langston Guest

    "ruffiano" <> wrote in message
    news:...
    > Hi, I was trying to use the string with memcpy.
    >
    > // void *memcpy( void *dest, const void *src, size_t count );
    >
    > Thanks again for the quick answers. This ng is really great.
    >
    >
    >
    > ruffiano wrote:
    >> Does the string class have a method for retrieving the address of a
    >> string?
    >>
    >> Thanks.


    It depends on what way you are going. If you are copying *from* a
    std::string to a char array, use .c_str()

    If you are copying from a char array *to* a std::string just do an
    assignment.

    std::string MyString;
    MyString = CharArray;
    Jim Langston, Aug 15, 2006
    #5
  6. ruffiano

    Default User Guest

    ruffiano wrote:

    > Hi, I was trying to use the string with memcpy.


    See below.



    Brian
    --
    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Aug 15, 2006
    #6
  7. ruffiano posted:

    > Does the string class have a method for retrieving the address of a
    > string?



    Yes.

    string *operator&();

    Demonstrated:

    string obj;

    string *p = &obj;

    If you don't trust a type to give you its real address, then:

    string obj;

    string *p = reinterpret_cast<string*>(
    &reinterpret_cast<char unsigned&>(obj) );


    If you want the address of a null-terminated string, try "string::c_str".

    --

    Frederick Gotham
    Frederick Gotham, Aug 15, 2006
    #7
  8. ruffiano

    Howard Guest

    "Frederick Gotham" <> wrote in message
    news:wBrEg.12711$...
    > ruffiano posted:
    >
    >> Does the string class have a method for retrieving the address of a
    >> string?

    >
    > If you don't trust a type to give you its real address, then:
    >
    > string obj;
    >
    > string *p = reinterpret_cast<string*>(
    > &reinterpret_cast<char unsigned&>(obj) );
    >


    Is that ever needed?

    More importantly, is it legal?

    -Howard
    Howard, Aug 15, 2006
    #8
  9. ruffiano

    Kai-Uwe Bux Guest

    Howard wrote:

    >
    > "Frederick Gotham" <> wrote in message
    > news:wBrEg.12711$...
    >> ruffiano posted:
    >>
    >>> Does the string class have a method for retrieving the address of a
    >>> string?

    >>
    >> If you don't trust a type to give you its real address, then:
    >>
    >> string obj;
    >>
    >> string *p = reinterpret_cast<string*>(
    >> &reinterpret_cast<char unsigned&>(obj) );
    >>

    >
    > Is that ever needed?


    Rarely, but keep in mind that operator& is overloadable. So, when you have a
    function

    foo ( T* ptr );

    and you call it like foo(&obj), it could happen that the compiler complains
    that &obj is not of type T* even though obj is of type T.


    > More importantly, is it legal?


    I think so. The boost version runs (essentially) like this:

    template <typename T>
    T* addressof ( T & v ) {
    return reinterpret_cast<T*>(
    &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
    }

    I never quite understood why they put in the const volatile just to cast
    ways the constness in the very next step.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 16, 2006
    #9
  10. Kai-Uwe Bux posted:

    >> More importantly, is it legal?


    Yes, it's perfectly legal. The Standard explicitly states that the
    following two expressions are equivalent:

    int a;

    double *p = &reinterpret_cast<double&>(a);
    and:
    double *p = reinterpret_cast<double*>(&a);

    We cast to a char*, which can hold the address of any object.


    > I think so. The boost version runs (essentially) like this:
    >
    > template <typename T>
    > T* addressof ( T & v ) {
    > return reinterpret_cast<T*>(
    > &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
    > }
    >
    > I never quite understood why they put in the const volatile just to cast
    > ways the constness in the very next step.



    So that it will compile if "T" is "int const volatile". If they ommited the
    "const volatile", then the following wouldn't compile:

    int main()
    {
    int const volatile a = 5;

    int *p = addressof(a);
    }

    --

    Frederick Gotham
    Frederick Gotham, Aug 16, 2006
    #10
  11. Frederick Gotham wrote:

    > Kai-Uwe Bux posted:
    >
    >>> More importantly, is it legal?

    >
    > Yes, it's perfectly legal. The Standard explicitly states that the
    > following two expressions are equivalent:
    >
    > int a;
    >
    > double *p = &reinterpret_cast<double&>(a);
    > and:
    > double *p = reinterpret_cast<double*>(&a);
    >
    > We cast to a char*, which can hold the address of any object.
    >
    >
    >> I think so. The boost version runs (essentially) like this:
    >>
    >> template <typename T>
    >> T* addressof ( T & v ) {
    >> return reinterpret_cast<T*>(
    >> &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
    >> }
    >>
    >> I never quite understood why they put in the const volatile just to cast
    >> ways the constness in the very next step.

    >
    >
    > So that it will compile if "T" is "int const volatile". If they ommited
    > the "const volatile", then the following wouldn't compile:
    >
    > int main()
    > {
    > int const volatile a = 5;
    >
    > int *p = addressof(a);
    > }


    This does not compile anyway. Make it

    int main()
    {
    int const volatile a = 5;

    int const volatile *p = addressof(a);
    }
    Markus Schoder, Aug 16, 2006
    #11
    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,210
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    877
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    428
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    768
    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:
    529
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page