OK, here I am.
First, make sure to click on the link I gave you, and
also read the link about Pointers and C-Strings. You are trying to do this:
1 2
|
char* foo; // Foo is a pointer to char, but it points to some random piece of memory.
foo[n] = 'H'; // NO! This will cause a crash!
|
You had it right the first time:
1 2
|
char foo[50];
foo[n] = 'H'; // Yay! So long as n is in 0..49 you are good to go!
|
Remember also that
*str
is the same as
str[0]
. If you want to check to see that
str points to something, you must check whether
its value is non-zero (or not
NULL
), not whether what it points to is non-zero. So, the correct test is:
if (str != NULL)
or, more succinctly,
if (str)
Now, on to the function of the algorithm. The
strtok() algorithm doesn't actually copy anything, so your function shouldn't either. Remember, if you read the link I gave you, remember that
strtok()
cheats -- it just sticks null characters ('\0') in the string and returns a pointer to the string you passed it! Hence, you don't need the
token variable. (Or the
i variable either, for that matter, but we'll get to that in a second.)
Next, the idea of your
place variable is a good one. (It is what I called "start" in the link I gave you.)
However, remember that you must either
give it a value, or
take a value from it. You cannot do neither. Also, you must give it a value before you take one. It is OK to do both.
To decide which, you must consider how
strtok() is called. We'll take it backwards because it is easier to consider this way:
... = strtok( NULL, ... );
The argument
str is NULL, so we must use the saved
place.
strtok( mystr, ... )
The argument
str is not NULL, so we do not need any saved
place. Before the function returns, make sure to set
place to the next place to use for
str if the function is called again with NULL as the argument to
str.
Avoid duplicating code. Right now, you have two branches (two
ifs): one for if
str is not NULL, and one for if it is. The only time the function behaves differently is if
str is initially NULL; the rest of the function does the same thing either way.
Read through the links I gave you, and try again, and post back.
Good luck!