char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
converting char * into char array
yeah...
How would I do this?
I have the following code from an example:
And I want to do it with str as a char * rather than an array, which would warrant some kind of conversion, probably.
My attempts at getting google to tell me fail miserably.
I know its a bit noobish but cany anybody please tell me simply how to do this?
#include <string>using namespace std;char str[] = "now # is the time for all # good men to come to the # aid of their country"; char delims[] = "#";std::string result;std::vector < std::string > resultarray; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims );resultarray.push_back(result); } for (int i=0; i< resultarray.size() ; i++ ) { printf(" result[%d]: %s", i, resultarray);
Possibly the easiest solution. Use std::string for all text related stuff, and std::vector <variable_type> for a general purpose dynamic array.
All my new code uses vector and string, but I have some old code here which was written in C. I want to modify it without porting it to c++.
ok... I have this:
char * data="now # is the time for all # good men to come to the # aid of their country";
char str[128];
strcpy(str,data);
Can you see any reason why this won't work? It seems to.
ok... I have this:
char * data="now # is the time for all # good men to come to the # aid of their country";
char str[128];
strcpy(str,data);
Can you see any reason why this won't work? It seems to.
Indeed, using a string class - whether it be std::string, CString, or your own string class, is almost always preferred over using char* or char[] for string manipulation.
However, to answer your question precisely. char[] and char* are largely interchangeable in C++, as a char[] is really just a "char* const". That is, an array of something is really just a constant pointer to data of the same type. This allows the data to be manipulated, while not allowing the address in memory to be changed.
With that said, you'd think you could just swap char[] for char* and get the following:
However, the above will not work. The reason it will not work, however, is not immediately obvious. The problem is that when you create a string using the above char* syntax, you're actually instructing the compiler to create a string literal, which is constant (cannot be modified). This is similar to "const char *". And, unfortunately, strtok attempts to chop up the string that's passed in, restoring the value before the function returns. As a result, the above will crash.
To make it work, the string that is passed into the strtok function must be dynamically allocated. The easiest way to do this is simply:
You can see here that it now works, because the value that's being passed to strtok is from a char* that can be modified (it's not a literal, but an in-memory pointer to a character string).
The conclusion of the story is that char* and char[] can be treated identically for read-only access. For write-access, char* and char[] can still be treated the same, so long as the char* is not constant, but is instead a pointer to an address on the free store.
Cheers!
However, to answer your question precisely. char[] and char* are largely interchangeable in C++, as a char[] is really just a "char* const". That is, an array of something is really just a constant pointer to data of the same type. This allows the data to be manipulated, while not allowing the address in memory to be changed.
With that said, you'd think you could just swap char[] for char* and get the following:
char* str = "now # is the time for all # good men to come to the # aid of their country";char delims[] = "#";char *result = NULL;result = strtok( str, delims );while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims );}
However, the above will not work. The reason it will not work, however, is not immediately obvious. The problem is that when you create a string using the above char* syntax, you're actually instructing the compiler to create a string literal, which is constant (cannot be modified). This is similar to "const char *". And, unfortunately, strtok attempts to chop up the string that's passed in, restoring the value before the function returns. As a result, the above will crash.
To make it work, the string that is passed into the strtok function must be dynamically allocated. The easiest way to do this is simply:
char* strLiteral = "now # is the time for all # good men to come to the # aid of their country";char* str = new char[strlen(strLiteral)];strcpy( str, strLiteral );char delims[] = "#";char *result = NULL;result = strtok( str, delims );while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims );}
You can see here that it now works, because the value that's being passed to strtok is from a char* that can be modified (it's not a literal, but an in-memory pointer to a character string).
The conclusion of the story is that char* and char[] can be treated identically for read-only access. For write-access, char* and char[] can still be treated the same, so long as the char* is not constant, but is instead a pointer to an address on the free store.
Cheers!
Yeah, this should work according to MSDN.
What i've meant with my previous code, is converting your string of tokens into an array of tokens, delimited with strtok.
What i've meant with my previous code, is converting your string of tokens into an array of tokens, delimited with strtok.
Quote:Original post by shotgunnutter
All my new code uses vector and string, but I have some old code here which was written in C. I want to modify it without porting it to c++.
ok... I have this:
char * data="now # is the time for all # good men to come to the # aid of their country";
char str[128];
strcpy(str,data);
Can you see any reason why this won't work? It seems to.
Why would you not port it since you already have done it with other parts of the program?
Anyways...
You forgot the o in strcopy(str,data).
Quote:Original post by shotgunnutter
All my new code uses vector and string, but I have some old code here which was written in C. I want to modify it without porting it to c++.
ok... I have this:
char * data="now # is the time for all # good men to come to the # aid of their country";
char str[128];
strcpy(str,data);
Can you see any reason why this won't work? It seems to.
This will work if you know your input is a fixed size, always less than 128 bytes (including the terminating null), and always a string literal, compiled into the executable.
But, if you're reading from a file, or requesting input from the user, you'd want to dynamically allocate your array (not fix it at 128 bytes), based on the size of the input.
However, if you're using a char* which is based on input from the user, chances are good it was already dynamically allocated, in which case there's no need to copy it to an array anyways, it can be passed directly to strtok.
If you're using strict, ANSI C, and dont want to use a C++ compiler, replace my 'new' calls with malloc, etc...Other than that, it should be legitimate C code.
Cheers!
Quote:Original post by ShlaklavaI don't believe there's a strcopy() in the standard library; I think it is in fact strcpy() (with no 'o').
Anyways...
You forgot the o in strcopy(str,data).
Right, I've got something I can work on now. Thanks everybody for explaining a couple of things.
Quote:Original post by shotgunnutter
yeah...
How would I do this?
I have the following code from an example:
*** Source Snippet Removed ***
And I want to do it with str as a char * rather than an array, which would warrant some kind of conversion, probably.
There is no reason why str can't be a char * instead of a char []. The only thing you have to be careful of is that strtok (cringe) modifies the string. So, that means that while str can be a char *, it must point to a non-const data and must not point to a literal string.
There is a handy C function named strdup (cringe) which will allocate memory and create a duplicate of the string. You can do something like this:
char const * str = "now # is the time for all # good men to come to the # aid of their country"; parse( str ); ... void parse( char const * str ) { char * copy = strdup( str ); char delims[] = "#"; char *result = NULL; result = strtok( copy, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); } free( copy ); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement