473,233 Members | 1,543 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,233 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 4295
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.