I try to compile with gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC)
The input file has a const char :
#include <stdio.h>
#include <stdlib.h>
#include "textfile.h"
...
const char * vs=NULL;
vs = textRead("myfile.file");
const char * vv = vs;
free(vs);
This chars vars are used into main cpp program and will be fill with text.
I use in header one function textRead to load the data from myfile.file
I got this error ( I thinking is a cast conversion ) but don’t know where I make mistakes.
warning: deprecated conversion from string constant to ‘char*’
error: invalid conversion from ‘const void*’ to ‘void*’
first warning is about vs = textRead(“myfile.file”);
second error is about free(vs);
What is wrong with my code ?
You haven’t posted what the
textReadfunction looks like, so I’m assuming its signature is as follows:textReadis taking the name of a file to read, it doesn’t (shouldn’t) need to modify the string passed to it, so changetextReadtoIf you cannot modify the function, change your code to
The error message about
freeis pretty self-explanatory,freeexpects avoid *and you’re passing it aconstpointer instead. I don’t see any reason whyvsneeds to be aconst char *instead of achar *. Change that and the error will go away.Also, I don’t understand why you’re making a copy of
vsright beforefreeing it, but maybe you haven’t posted stuff that happens in between those 2 lines.