Sign in to follow this  
geKingu

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 this post


Link to post
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 function
int 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 this post


Link to post
Share on other sites
geKingu    122
Thanks for the reply phresnel.

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

/*a simple client
it 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 host

IPaddress ipaddress;//the ip address
TCPsocket tcpsock;//the socket to use

char nomeserver[50];//max dimension of the name of server

int result;//to store the result of some functions
int len;//the lenght of the message to send
char messaggio [ DIM ];//this is our buffer
/************************/


//init sdl

if (SDL_Init(0) < 0 ){printf("no init sdl\n"); exit ( -1);}
else { printf("sdl init : ok\n");}


//init sdl_net

if (SDLNet_Init() < 0 ){printf("no init sdlnet\n"); exit ( -1);}
else { printf("init sdlnet : ok\n");}

//scanf for the name of server

printf("Name (IP) server : \n");
scanf("%s",&nomeserver);
printf("porta (default) : %d\n",PORTA);

//try to resolve the name

if ( SDLNet_ResolveHost(&ipaddress,nomeserver,1234) < 0 ){printf("no resolve\n");
printf("continue anyway\n");}
else { printf("resolve : ok\n");}



//try to resolve the ip

if ( (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 loop

while (1)
{
//scanf for the message to send

scanf("%s",&messaggio);

//calculate lenght and send it

len=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 infinito





system("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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this