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

Address of a string

P: n/a
Does the string class have a method for retrieving the address of a
string?

Thanks.

Aug 15 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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!!!
Aug 15 '06 #2

P: n/a
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
Aug 15 '06 #3

P: n/a
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.
Aug 15 '06 #4

P: n/a
"ruffiano" <ro************@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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;
Aug 15 '06 #5

P: n/a
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>
Aug 15 '06 #6

P: n/a
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
Aug 15 '06 #7

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:wB*******************@news.indigo.ie...
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

Aug 15 '06 #8

P: n/a
Howard wrote:
>
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:wB*******************@news.indigo.ie...
>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
Aug 15 '06 #9

P: n/a
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
Aug 15 '06 #10

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

Aug 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.