Domanda

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?

È stato utile?

Soluzione

As in the miniunz.c file from Zip Code.

The function definition is as follows:

int makedir (newdir)
    char *newdir; 

So by considering that, There are two ways to do this.

  char* write_filename;

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

OR

  const char* write_filename;

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

Or Check your makedir() function.

Hope this will help you.

Altri suggerimenti

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.

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top