In C the type char
is incompatible with the type char *
. Simply because they are different types all together. A char
is, by definition, guaranteed to be the smallest type (with a size of 1). A char *
has the same size as any pointer: it has to be big enough to accomodate a memory address: typically 4 on a 32bit system, 8 on 64bit.
You are assigning a string constant to a variable declared as a char
, the assignment expects the right hand argument to be something along the lines of:
char foo = 'a';//single quotes indicate a char
Or, perhaps more what you had in mind:
const char *foo = "April.txt";//double quotes indicative of char *
Note the const
storage class: the string constant will not be assigned to the variable, instead the value of filename
will be the memory address of the string, which is stored in read only memory. Hence, it's a const
: you cannot alter it:
const char *foo = "foobar";
foo[0] = 'F';//<-- ERROR
To be able to modify the value, write:
char foo[] = "foobar";
foo[0] = 'F';//Success!
This will create an array, large enough to accommodate the string, and copy the chars. Of course, if the value of your var is likely to change, it is possible for it to be large enough to accommodate larger strings, too, in which case:
char foo[100] = "up to 100 chars";
strcat(foo, "!!");
Or, using dynamic memory (but avoid, if at all possible, because heap is slower and requires more attention from you):
char *foo = malloc(20);//begin with 20 chars
if (foo == NULL)
exit( EXIT_FAILURE);//no memory could be allocated
strcpy(foo, "foobar");//copy string value into allocated memory
foo = realloc(foo, 50);//increase amount of allocated memory to hold 50
if (foo == NULL)//safety first:
exit(EXIT_FAILURE);
strncat(foo, "some huuuuuuge string", 30);//strncat for safety, again
Ah well, there's a variety of ways to do things, depending on your specific needs