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?
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 eg when you apply the sizeof
operator on a pointer and on an array: on an array you get the array size (ie 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 (ie 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, ie 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 (ie 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.
我认为您必须像这样将您的name []数组传递给strtok:
strtok(&name[0],"n");
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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.