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

11,908

Solution 1

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.

Solution 2

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.

Share:
11,908
HDdeveloper
Author by

HDdeveloper

Updated on June 22, 2022

Comments

  • HDdeveloper
    HDdeveloper almost 2 years

    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?

  • user694733
    user694733 over 10 years
    You don't need to cast for fopen. Also, I would really recommend not casting const away from char *. If makedir modifies the string, and you pass literal string which is placed in read-only memory, you will get undefined behaviour. It is not portable code. Proper way would be to make non-const copy of string for the function that needs it.