How to convert a string to const char *?

6

I'm trying to make an audio player (an mp3 player) using the SDL_mixer library.

The problem is that I have a function that returns a string (a directory of a song) that I need to pass this string as an argument to the function *Mix_LoadMUS(const char *file) .

I am a beginner in programming (1st period), I will be grateful if the answers are simplified.

How do I transform a string into a type const char* ?

    
asked by anonymous 19.02.2014 / 04:30

3 answers

1

If it is GTK + GString, use

astringemquestao->str

that points directly to the buffer containing the string. It is null terminated and can be used where const char * is expected.

Remembering that: any modification in GString, the value of member str can change. Therefore this pointer should not be reused. If you need access to the string for a longer time, during which the GString might change, make a copy and use the copy, not forgetting to release later.

    
24.02.2014 / 04:09
1

If you are using a std (string) string, call the c_str () function.

For example:

std::string str;

...

Mix_LoadMUS(str.c_str());
    
12.03.2014 / 22:47
0

The const qualifier only indicates that the variable can not be changed. So the code below means that the data pointed to by the file pointer is not changeable:

void Processa(const char * file)
{
      ....
}

The compiler takes care of generating an error alert if the code in the routine tries to change the content pointed to by the pointer. Note that const char * p or char const * p has the same meaning, ie the data pointed to is not modifiable. But char * const p means the pointer is constant, and can not be modified. So it should be no problem, you pass a string variable without const qualifier to a routine like the one shown in your question. [EDIT]
Rereading your question I realized that your problem is that the parameter you are trying to pass into the routine is generating the error at compile time, correct? If yes, try using casting to adjust the parameter to the required by the calling routine, for example:

   char *p = Mix_LoadMUS ( (const char *) file )
    
19.02.2014 / 12:28