# Cannot convert from 'const char *' to 'char *'

## Recommended Posts

geKingu    122
Hey people. I'm trying to compile some code posted by honny in This Thread. When compiling the server code, I get the error:
error C2440: '=' : cannot convert from 'const char *' to 'char *'
Error is being produced from line 75 which is:
if ( (host = SDLNet_ResolveIP (&ipaddress) ) == NULL ){printf("WARNING : no name ! ! !\n");
I'm unsure as to what the problem is so any help would be appreciated! ~geKingu

##### Share on other sites
phresnel    953
Could you please post some more lines of context, or the whole function (especially because you have written both the condition as well as the corpus of the if-statement in a single line)? So we can see all variable declarations, et cetera.

But maybe this helps: If you can ensure that what you want to cast to 'char *' won't be changed, const_cast maybe helps:

// some old C functionint foo (char *s) {    // Do something, except changing the content of s    ...}...int main () {    const char *s = "All your base are belong to us!";    int bar = foo (const_cast <char *> (s));    ...}

Use const_cast if and only if you know that it won't be changed in any function you pass it to. Otherwise you get undefined behaviour:

void foo (char *s) {    s [5] = '\0';   // UNDEFINED  BEHAVIOUR IF s IS ACTUALLY CONST.}...int main () {    const char *s = "All your base are belong to us!";    int bar = foo (const_cast <char *> (s)); // DON'T DO, foo() is gonna change s!    ...}

The reason there is const_cast is simply to be able to reuse old c-functions which are not const-correct. It can work, but is also perfectly allowed to explode your computer.

[Edited by - phresnel on December 17, 2008 3:43:31 AM]

##### Share on other sites
geKingu    122

The client code has the exact same single error, and as the client code is shorter I'll post that instead:

/*a simple clientit connect to the server and start to send messages*/#define DIM 1024	//our max dimension#define PORTA 1234//the port which we want to listen on/*standard headers*/#include <stdlib.h>#include <stdio.h>#include <process.h>#include <direct.h>#include <string.h>/*sdl headers*/#include "SDL.h"#include "SDL_net.h"/************************/int main(int argc, char *argv[]){char *host;//where we store the name of hostIPaddress ipaddress;//the ip addressTCPsocket tcpsock;//the socket to usechar nomeserver[50];//max dimension of the name of serverint result;//to store the result of some functionsint len;//the lenght of the message to sendchar messaggio [ DIM ];//this is our buffer/************************///init sdlif (SDL_Init(0) < 0 ){printf("no init sdl\n"); exit ( -1);}else { printf("sdl init : ok\n");}//init sdl_netif (SDLNet_Init() < 0 ){printf("no init sdlnet\n"); exit ( -1);}else { printf("init sdlnet : ok\n");}//scanf for the name of serverprintf("Name (IP) server : \n");scanf("%s",&nomeserver);printf("porta (default) : %d\n",PORTA);//try to resolve the nameif ( SDLNet_ResolveHost(&ipaddress,nomeserver,1234) < 0 ){printf("no resolve\n"); 														printf("continue anyway\n");}else { printf("resolve : ok\n");}//try to resolve the ipif ( (host = SDLNet_ResolveIP (&ipaddress) ) == NULL ){printf("no ip\n"); 													printf("continue anyway\n");}else { printf("connesso a : %s\n",host);}//try to open the socket//if it fails no way : WE MUST QUIT !!!tcpsock = SDLNet_TCP_Open(&ipaddress);if (!tcpsock){printf("no TCP\n"); exit ( -1);}else { printf("TCP : ok\n");}printf("dear user start to write your messages\n");printf("and press enter to send it :\n");//this is the main loopwhile (1){//scanf for the message to sendscanf("%s",&messaggio);//calculate lenght and send itlen=strlen(messaggio)+1;result = SDLNet_TCP_Send(tcpsock,messaggio,len);printf("send byte  : %d\n", result);printf("send : %s\n",messaggio);printf("write a new message :\n");																};//chiude ciclo infinitosystem("pause");SDL_Quit();return 0;

and the error(s):

\main.cpp(50) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.\main.cpp(63) : error C2440: '=' : cannot convert from 'const char *' to 'char *' Conversion loses qualifiers

As you can see, the error occurs when trying to resolve the host's ip. I'm quite new to programming so I am confused as to why the error has occurred. Neither *host or ipaddress are constants?

edit: typo :P

##### Share on other sites
Rattenhirn    3114
SDLNet_ResolveIP returns a "const char*" which you try to assign to "host", which is declared as "char*". Casting from non-const to const is done implicitely, as there is no harm. The other way round, like you are trying to do, has to be done explicitely and should be avoided as much as possible.

In your case the fix is simple: make "host" a "const char*".

##### Share on other sites
phresnel    953
Rattenhirn is right. Something like
const char *s = SDLNet_ResolveIP(...)
should work.

Here's the exact signature of the function.

##### Share on other sites
HomerSp    126
A const char pointer cannot be cast to a char pointer because a const is supposed to be constant, E.G. not changeable.

##### Share on other sites
geKingu    122
Thanks for all the replies! Changing *host to a const char worked :) I never knew SDL_Net_ResolveIP returned a const :P

Silly me.

##### Share on other sites
shurcool    439
One thing to note about "const char *s" is that it means a *pointer* to a 'const-char'. It is not a *const-pointer* to a char.

That is why you're able to assign a value to the pointer, as the pointer itself isn't const. But it points to a const-char.

The reason the compiler gave you an error before was to prevent you to modify the const-char the pointer points to, since your original pointer was pointing to a non-const char.

Edit: I don't know if that made sense, so feel free to ignore this if I only confused you. I've basically repeated what everyone else has already mentioned here. ;/

P.S. I'm only 90% sure about what I said above being correct. I didn't confirm it and my memory might be off.

##### Share on other sites
geKingu    122
Thanks shurcool, you cleared things up for me! I got confused as I assumed const char *host was a const pointer to a char! It makes much more sense now :)

##### Share on other sites
shurcool    439

You can take a look at question 18.5 (and the one above) of C++ FAQ Lite to see what other combinations are possible.