pinter = string.

J

JoeC

I have a simple question, how do I move data from a string to a
pointer array. I am doing some work in win32 and many of the commands
need pointers but poitner are cumbersome and I try to avoid them.

Basically I want to do:
char * prt;
std::string str;
str = "Hello";
ptr = str;
 
J

John Harrison

JoeC said:
I have a simple question, how do I move data from a string to a
pointer array. I am doing some work in win32 and many of the commands
need pointers but poitner are cumbersome and I try to avoid them.

Basically I want to do:
char * prt;
std::string str;
str = "Hello";
ptr = str;

Not possible, but this is possible, using const is the vital difference

const char * ptr;
std::string str;
str = "Hello";
ptr = str.c_str(); // use c_str to get a *const* pointer

If you really want a non-const pointer (i.e. you want to use the pointer
to modify the string) then you are going to have to look elsewhere,
vector<char> might be what you need.

john
 
V

Vitor

John said:
Not possible, but this is possible, using const is the vital difference

const char * ptr;
std::string str;
str = "Hello";
ptr = str.c_str(); // use c_str to get a *const* pointer

If you really want a non-const pointer (i.e. you want to use the pointer
to modify the string) then you are going to have to look elsewhere,
vector<char> might be what you need.

john

I will have to see because using const will make me modify the program
quite a bit. I will see if the win32 commands accept the const char and
I will just use string through out my program.
 
G

Gavin Deane

Not possible, but this is possible, using const is the vital difference

const char * ptr;
std::string str;
str = "Hello";
ptr = str.c_str(); // use c_str to get a *const* pointer

If you really want a non-const pointer (i.e. you want to use the pointer
to modify the string) then you are going to have to look elsewhere,
vector<char> might be what you need.

There is one situation where the const issue can be bypassed (and I
suspect it may come up with the Windows API).

To the OP:
As John says, you can't do
char* ptr = str.c_str();

but you can do
const char* ptr = str.c_str();

You can also do
char* ptr = const_cast<char*>( str.c_str() );
However, while you have bludgeoned the compiler into giving you a
pointer to non-cost char, you can't actually use that pointer to
modify data in the string. So the question is, why would you do this.
Answer: in your own code there's not likely to be any reason at all to
do this. But using some third party API you might find yourself faced
with needing to call functions like this

void some_API_function(char* c)
{
/*
This function doesn't actually modify the data pointed to by c. The
parameter should be const char* c but, for whatever reason, the const
has been left out [*].
*/
}

If you are *absolutely certain* that some_API_function really will not
ever, under any circumstances, modify the data pointed to by its
parameter, you can safely call it as below, allowing you to use
std::string within your own code.

some_API_function( const_cast<char*>(str.c_str()) );

[*] Reasons the const might have been left out:
1. The API is old enough to predate the widespread use of const.
2. The API is designed to be available to C and C++ programs. C did
not originally have const at all.
3. Carelessness on the part of the programmer who wrote the API.

Gavin Deane
 
Z

Zachary Turner

Not possible, but this is possible, using const is the vital difference

const char * ptr;
std::string str;
str = "Hello";
ptr = str.c_str(); // use c_str to get a *const* pointer

If you really want a non-const pointer (i.e. you want to use the pointer
to modify the string) then you are going to have to look elsewhere,
vector<char> might be what you need.

john


If performance isn't critical, it might be advisable to use
vector<char> as mentioned above and then copy the results into a
string using code such as:

vector<char> buf(MAX_PATH);
SomeApiThatRequiresCharStar(&buf[0], MAX_PATH);
std::string result = &vec[0];

If performance is critical you can consider const_cast, although you
should be somewhat careful. In situations where I'm 100% sure that
the API will not write anything whatsoever to the string, I've been
known to break the rule and go ahead and use it to save the overhead
of having to declare a new vector and copy memory around like that.
Like this:

std::string filename = "whatever.txt";
::CreateFile(const_cast<char*>(filename.c_str()));

Make sure you know what you're doing though, or you may to introduce
some extremely difficult to find errors.
 
J

James Kanze

Not possible, but this is possible, using const is the vital difference
const char * ptr;
std::string str;
str = "Hello";
ptr = str.c_str(); // use c_str to get a *const* pointer
If you really want a non-const pointer (i.e. you want to use the pointer
to modify the string) then you are going to have to look elsewhere,
vector<char> might be what you need.

I tend to use vector<char> for such things myself, but the
following will work with all existing implementations of
std::string, and will be guaranteed in the next version of the
standard:

std::string str ;
str.resize( targetSize ) ;
char* p = &str[ 0 ] ;

You just have to be careful about the size.
 
J

Jim Langston

Vitor said:
I will have to see because using const will make me modify the program
quite a bit. I will see if the win32 commands accept the const char and I
will just use string through out my program.

There are a lot of C style functions that expect a char* and aren't const
correct. That is, they don't specify const even though they won't modify
the string. If you are absolutely sure that the function won't modify the
string, then you can throw away the constantness.

std::str str;
str = "Hello";
SomeFunctionNotConstCorrect( const_cast<char*>( str.c_str() ) );
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top