Hi to all...

I am getting a warning, i am trying to avoid that but its not sorted out..Please suggest me to avoid that warning.

my code is as follows..

const char var[][2][5]={ 
                                 {"1.95","3.70"},
				 {"2.40","4.60"},
				 {"2.70","5.70"},
				 {"6.60","13.40"},
                                        .
                                        .
                                        .
                                        .
                                };
  
  void fun(int col,int row,char *str)
  {
     ...
     ...   //(here i am printing that string...)
  }

  main()
  {
     fun(var[45][1]);
  }

My definition of fun function, array, main function are in the same file.....

main.c(11498): warning: passing arg 3 of `fun' discards qualifiers from pointer target type

So this is the warning i am facing...please suggest me how can i avoid that warning...

Dani AI

Generated

The warning means two separate issues combined: the compiler sees you passing a pointer to const data into a parameter declared as a non-const pointer (that "discards qualifiers" message), and your call is only supplying one argument while fun is declared to take three. Treating read-only strings as writable is unsafe — the compiler warns you so you don't accidentally modify memory that should be immutable.

Fix checklist (in order):

  • If fun only reads the string, make the third parameter const char * so the qualifier isn't discarded.
  • Call fun with the correct number of arguments (column, row, and the string pointer).
  • Make sure each string storage has room for the terminating NUL (length + 1).
  • If fun must modify the string, copy the text into a writable buffer and pass that buffer — do not cast away const.

Example patterns (different from the originals above):

/* read-only parameter */
void fun(int col, int row, const char *s) {
    puts(s);
}

/* call with column and row */
fun(45, 1, arr[45][1]);
/* if modification is needed, copy first */
char tmp[32];
strncpy(tmp, arr[45][1], sizeof tmp - 1);
tmp[sizeof tmp - 1] = '\0';
fun(45, 1, tmp);

Don't silence the warning with a cast like (char *); that hides real problems. Enable compiler warnings (-Wall -Wextra), add a proper prototype before calls, and prefer const for read-only strings — as suggested, it documents intent and removes the warning.

Recommended Answers

All 2 Replies

look at the argument list for the function fun() -- it requires three parameters; two integers and a char*. Now look at how you are trying to call that function in main(). It is only passing one parameter instead of three.

This compiles correctly with vc++ 2010 express. Note the last dimension must be 6 instead of 5 because "13.40" requires 6 bytes including the null terminator.

const char var[][2][6]={ 
    {"1.95","3.70"},
    {"2.40","4.60"},
    {"2.70","5.70"},
    {"6.60","13.40"}
};

void fun(int col,int row,const char *str)
{
}
int main()
{
    fun(0,0,var[0][0]);
}

Thank you..very much for your quick response....i managed to avoid them....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.