I didn't understande the concept of (char*)str.c_str()?

what does str.c_str() func does?

hello @sahasoumyajit2020

see string is a class in c++ but strtok takes char * in its input .
so thats why we are using c_str function . it converts the c++ string into c based string(char *) and returns its base address.

then why are we again type casting it with (char*)?

c_str() is a function that converts c++ string into char array(c type string) and return its base address as( const char *).

now look at the declaration of strtok.

ss

here it takes char * type pointer but we have const char * pointer
so first we need to explicitly convert const char * to char *
thats the reason why we have (char*) str.c_str()

char *s = strtok( (char * ) str.c_str() , " ");

ohh thnks now i got it

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.