473,327 Members | 2,118 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,327 software developers and data experts.

pinter = string.

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;

Jun 21 '07 #1
6 1624
JoeC wrote:
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<charmight be what you need.

john
Jun 21 '07 #2
John Harrison wrote:
JoeC wrote:
>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<charmight 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.
Jun 21 '07 #3
On 21 Jun, 23:16, John Harrison <john_androni...@hotmail.comwrote:
JoeC wrote:
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<charmight 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

Jun 21 '07 #4
On Jun 21, 5:16 pm, John Harrison <john_androni...@hotmail.comwrote:
JoeC wrote:
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<charmight be what you need.

john

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

vector<charbuf(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.

Jun 22 '07 #5
On Jun 22, 12:16 am, John Harrison <john_androni...@hotmail.com>
wrote:
JoeC wrote:
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<charmight be what you need.
I tend to use vector<charfor 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.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 22 '07 #6
"Vitor" <no**@yahoo.netwrote in message
news:46***********************@roadrunner.com...
John Harrison wrote:
>JoeC wrote:
>>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<charmight 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.
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() ) );
Jun 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.