var1 must hold a string whether it is empty or not or my program will segfault. But gcc complains that the empty string literal is constant while var1 is not. The following is an example of what I am talking about.
How can I fix this ?
warning: assignment discards qualifiers from pointer target type
char *var1 = NULL;
if(var1 == NULL)
{
var1 = malloc(strlen(var2) + 1);
strcpy(var1, var2);
}else{
var1 = ""; // warning points here
}
EDIT:
String literals are not modifiable so I use this flag to warn me:
-Wwrite-strings
To assign a empty string, do something like this: