473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Address of a string

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

Thanks.

Aug 15 '06 #1
10 4301
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
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
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
"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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gidraz | last post by:
Hello, i can't figure out how to retrieve clients MAC address using C#.NET. I can get IP but not MAC. Thanks in advance. Gidraz
0
by: MikeM | last post by:
The email address stays through each loop and keeps adding the next email address. joe@email.com then the next run would be joe@email.com; mike@email.com etc... The subject and the attached file...
8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
11
by: Brian Henry | last post by:
I have a domain cluster with AD running, and I want to lookup a users email address (exchange 2000 server is integrated with the AD system) so i can email the user based on their user name. does...
19
by: Manish Tomar | last post by:
Hi All, The following code as per my knowledge should not work: int* some() { int b = 10; return &b; }
0
by: Avi | last post by:
A CreateUserWizard contorl receives new user details, adds the user to the database, tries to send a confirmation mail message and stops with a "System.FormatException: The specified string is not...
5
by: ttoboobz | last post by:
Help anyone... I'm creating a simple address book java program that would store 100 records. The GUI should be able to accept the name, address, phone no., and email add of a person by clicking a...
4
by: karen.homersham | last post by:
Just working through Apress Pro ASP NET.2.0.E Commerce in C Sharp. I am having problems compiling the following: using System; using System.Collections.Generic; using System.Text; namespace...
1
by: zhang | last post by:
what's the problem?? Remote_Addr = "hotmail.com" sFrom = "<makefriend8@" & Remote_Addr + ">" Dim oConnection As New TcpClient() Try oConnection.SendTimeout = 3000 ...
2
by: Richard Maher | last post by:
Hi, Can someone please tell me the strategy(ies) used by Java (the Security Manager or whatever) to determine if a given IP address conforms to the definition of the codebase from which an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.