Sign in to follow this  

converting char * into char array

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

yeah... How would I do this? I have the following code from an example:
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 );
   } 



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?

Share this post


Link to post
Share on other sites

#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[i]);



Possibly the easiest solution. Use std::string for all text related stuff, and std::vector <variable_type> for a general purpose dynamic array.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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:


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!

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Quote:
Original post by Shlaklava
Anyways...

You forgot the o in strcopy(str,data).
I don't believe there's a strcopy() in the standard library; I think it is in fact strcpy() (with no 'o').

Share this post


Link to post
Share on other sites
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 );
}

Share this post


Link to post
Share on other sites

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this