0
int main()  
{
    char name[]="avinash";   
    const char* nameano="a";   
    strtok(name,"n");   
    cout<<"the size of name is"<< sizeof(name);   
    cout<< name;
} 

strtok takes in arguments (char*, const char*); name is an array, and hence a pointer to its first element. But if we make a declaration like

string name="avinash";

and pass name as first argument to strtok, then the program doesn't work, but it should, because name, a string, is a pointer to its first character.

Also, if we write

const string n = "n";

and pass it as second argument it doesn't work; this was my first problem.

Now also the sizeof(name) output is 8, but it should be 4, as avinash has been tokenized. Why does this happen?

4
  • 2
    I don't believe sizeof(name) should change. Note that sizeof is not the same as strlen... Commented Apr 8, 2011 at 12:43
  • 1
    You should really learn how to format you code and text in a question (See the editing help on the "ask question" page, and use less "..." in you text). Also, it's not very clear what exactly you are asking. Commented Apr 8, 2011 at 12:53
  • I have cleaned up the question, hope I got everything right. Commented Apr 8, 2011 at 13:02
  • @matteo italia....thanks for editing Commented Apr 8, 2011 at 16:53

5 Answers 5

5

You are confusing several things.

strtok takes in arguments (char*, const char*)....name is an array and hence a pointer to its first element...

name is an array, and it's not a pointer to its first element. An array decays in a pointer to its first argument in several contexts, but in principle it's a completely different thing. You notice this e.g. when you apply the sizeof operator on a pointer and on an array: on an array you get the array size (i.e. the cumulative size of its elements), on a pointer you get the size of a pointer (which is fixed).

but if we made a declaration like string name="avinash" and passed name as argument then the prog doesnt work but it should because name of string is a pointer to its first character...

If you make a declaration like

string name="avinash";

you're are saying a completely different thing; string here is not a C-string (i.e. a char[]), but the C++ std::string type, which is a class that manages a dynamic string; those two things are completely different.

If you want to obtain a constant C-string (const char *) from a std::string you have to use it's c_str() method. Still, you can't use the pointer obtained in this way with strtok, since c_str() returns a pointer to a const C-string, i.e. it cannot be modified. Notice that strtok is not intended to work with C++ strings, since it's part of the legacy C library.

also if we write const string n = "n"; and pass it as second argument it doesnt work...this was my first problem...

This doesn't work for the exact same motivation, but in this case you can simply use the c_str() method, since the second argument of strtok is a const char *.

now also the sizeof(name) output is 8 but it should be 4 as avinash has been tokenised..

sizeof returns the "static" size of its operand (i.e. how much memory is allocated for it), it knows nothing about the content of name. To get the length of a C-string you have to use the strlen function; for C++ std::string just use its size() method.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you have to pass your name[] array to strtok like this:

strtok(&name[0],"n");  

1 Comment

If name is a C-string, the syntax used by the OP (strtok(name, "n")) is already ok. If, instead, name is an std::string, you are potentially violating the preconditions of strtok, since nothing guarantees you that it's internal buffer is NULL-terminated (and actually it could even contain a NULL before the end of the string, since std::strings can contain any character).
0

First of all, strtok is C and not C++ and is not made to work with C++ strings. Plus, it cant work if you dont use the return result of the function.

char name[]="avinash";   
char * tok_name = strtok(name,"n");   
std::cout<<"the size of name is"<< sizeof(tok_name);   
std::cout<< tok_name;

You should consider to use std::string::find, std::string::substr and other stuff from the STL library instead of C functions.

4 Comments

strtok can be used in c++ as well.
Right, but it is a bad idea imho.
strtok is C++, for the same reason that most of the C library is C++. Still, strtok was already broken in C, and shouldn't have been used there, much less in C++. And C++ has a lot of more powerful tools to achieve the same ends: both the member functions of std::string and the algorithms in the standard library.
C functions are usable in C++. For me, it does not mean that they are C++. I think a lot of people will agree that C++ is not exactly an extension of C. This is an old debate ^^
0

As you said, strtok takes arguments of type char * and const char *. A string is not a char * so passing it as the first argument will not compile. For the second argument, you can convert a string to a const char * using the c_str() member function.

What problem are you trying to solve? If you're just trying to learn how strtok works, it would be much better to stick to raw character arrays. That function is inherited from C and thus wasn't designed with C++ strings in mind.

Comments

0

I don't believe the actual size of the name array is ever changed when you call strtok. The call remembers the last location it was called if you pass it a null pointer, and it continues to tokenize the string. The return value of the strtok call is the token that has been found in the string provided. Your code is calling sizeof(name) which is never actually adjusted by the strtok function.

int main()
{
   char name[] = "avinash";
   const char* nameano = "a";
   char* token;
   token = strtok(name, "n"); 
   cout << "the length of the TOKEN is" << strlen(token) << endl;
   cout << token << endl;
   cout << "the length of the string is" << strlen(name) << endl;
   cout << name << endl;
}

Try this maybe? I am not in front of a compiler so its likely I made a mistake, but it should get you on the right track to solve this problem.

This might also help: http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.