Question : how to cast string to char*?

Hi,
Im using token and I wanted to store the token value into a string in case I needed it when I move to next token but the thing is I have to handle this new string with a function that expects char* so how can I cast it back to be char*?

FILE *source;
char *token;
string name;
      fgets(record,MAX_LEN, source);
           
            /* Parse into tokens */
            token = strtok(record, " \t\n\r");
name = token;
token = strtok(NULL, " \t\n\r"); //go to next token

//here I decided I need to assign name to another function that take char*
add_symbol(char *label);

how can I cast the name to be of type char again so I can use add_symbol function
to added, I have seen somewhere that I can cast the string to be const char* but im not sure how to do that

appreciate your help,
thanks,

Answer : how to cast string to char*?

To convert a string to a const char*, you can use the c_str() method:
    http://www.cplusplus.com/reference/string/string/c_str/
Below is the example taken from this link.

I'm not sure that I understand the entirety of your question; and if you want to expand upon that to show what you tried to do and how it is going wrong (by using a main test driver showing the actual output vs the desired output), that would be fine.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main ()
{
  char * cstr, *p;

  string str ("Please split this phrase into tokens");

  cstr = new char [str.size()+1];
  strcpy (cstr, str.c_str());

  // cstr now contains a c-string copy of str

  p=strtok (cstr," ");
  while (p!=NULL)
  {
    cout << p << endl;
    p=strtok(NULL," ");
  }

  delete[] cstr;  
  return 0;
}
Random Solutions  
 
programming4us programming4us