'Passing 'const char *' to parameter of type 'char *' discards qualifiers

I am getting warning:

miniunz.c:342:25: Passing 'const char *' to parameter of type 'char *' discards qualifiers

in miniunz.c file of the Zip Archive library. Specifically:

const char* write_filename;
fopen(write_filename,"wb"); //// This work fine...........
makedir(write_filename);    //// This line shows warning....

How should this warning be removed so that both work fine?



Solution 1:[1]

An improvement upon the original answer:

You have a function which takes an NSString * as a parameter.

First you need to convert it to a const char pointer with const char *str = [string UTF8String];

Next, you need to cast the const char as a char, calling

makedir((char*)write_filename);

In the line above, you're taking the const char value of write_filename and casting it as a char *and passing it into the makedir function which takes a char * as its argument:

makedir(char * argName)

Hope that's a bit clearer.

Solution 2:[2]

The parameter being passed to makedir is of type char* and not of type const char*.

The warning you're getting is warning you that passing a value with the const qualifier (in this case, write) to a parameter without it removes the const qualifier from the value. One way to avoid this warning is manually removing the const qualifier from write_filename, as below:

char* write_filename;
fopen(write_filename,"wb");
makedir(write_filename);

However, I don't recommend this, because you may want to keep write_filename const. If you want to avoid this warning while keeping write_filename const, you can manually cast it to a normal char*, as below:

const char* write_filename;
fopen(write_filename,"wb");
makedir((char*) write_filename);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zack Shapiro
Solution 2 William Powell