trying to compile stir library: error: invalid conversion from ‘const char*’ to ‘char*’

First, I'm pretty new to C++ and C so be easy on me :-) Second, I know this question has be asked many times in many forms before, but I could figure how to bend the answers to my case ...

I am trying to compile a file called utilities.cxx from the STIL library which has some kind of and "open source" license (not really LGPL and so I don't know if I can put here significant parts of it...

The code has the following function in it:

char *replace_extension(char *file_in_directory_name, 
            const char * const extension)
char * location_of_dot = 
// first truncate at extension
if (location_of_dot!= NULL)
*(location_of_dot) = '\0';

strcat (file_in_directory_name,extension);
return file_in_directory_name;

Compiling it gives the error:

g++  -O3  -ffast-math -DNDEBUG  -Wall -Wno-deprecated -I../lmf_v2.0
-o  opt/buildblock/utilities.o -MMD -MP -c buildblock/utilities.cxx 
buildblock/utilities.cxx: In function ‘char* stir::replace_extension(char*, const 
buildblock/utilities.cxx:225: error: invalid conversion from ‘const char*’ to ‘char*’
make: *** [opt/buildblock/utilities.o] Error 1

Any help would be appreciated ... Thanks,


Ok, first part already answered ... here is the function which causes the second error:

const char * const 
find_filename(const char * const filename_with_directory)
const char * name;

#if defined(__OS_VAX__)
name = strrchr(filename_with_directory,']');
if (name==NULL)
name = strrchr(filename_with_directory,':');
#elif defined(__OS_WIN__)
name = strrchr(filename_with_directory,'\\');
if (name==NULL)
name = strrchr(filename_with_directory,'/');
if (name==NULL)
name = strrchr(filename_with_directory,':');
#elif defined(__OS_MAC__)
name = strrchr(filename_with_directory,':');
#else // defined(__OS_UNIX__)
name = strrchr(filename_with_directory,'/');
if (name!=NULL)
// KT 10/01/2000 name++ changed to name+1
return name+1;
return filename_with_directory;


This line is causing the error:

char * location_of_dot = 

strchr() returns a const char*, not a char* when called with a const char* as the first argument (I'm assuming find_filename() returns a const char *, otherwise you wouldn't be seeing this error).

Since you want to assign to the memory location returned by strchr, you don't want to use this overloaded version. Change find_filename() to return a char*.

UPDATE: You've since posted the code for find_filename() and changing the return type would involve changing other stuff (and doesn't make much sense besides). Instead, cast either the return value of find_filename() to a char* or cast the result of strchr() to a char*.

Example (uses a const cast):

char * location_of_dot = const_cast<char*>(

Need Your Help

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.