Address of a string

R

ruffiano

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

Thanks.
 
P

Phlip

ruffiano said:
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...
 
V

Victor Bazarov

ruffiano said:
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
 
R

ruffiano

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.
 
J

Jim Langston

ruffiano said:
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.

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;
 
F

Frederick Gotham

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".
 
H

Howard

Frederick Gotham said:
ruffiano posted:


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
 
K

Kai-Uwe Bux

Howard said:
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
 
F

Frederick Gotham

Kai-Uwe Bux posted:

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);
}
 
M

Markus Schoder

Frederick said:
Kai-Uwe Bux posted:


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.




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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top