Jordan Tran Jordan Tran - 5 months ago 12
C Question

In c Argument of type char is incompatiple with parameter type const char

I am currently getting the title error within this block of code.

void Option_3(char filename)
{ // profit for a month

FILE *ptr_file;
int num1, num2, num3;
int a[31],b[31],c[31];
int count=0;
int i;

ptr_file = fopen ( filename ,"r"); // error is here at file name

later the main

void main()
int select;
char filename;
select = 0 ;
filename = "April.txt"; //here the equals sign is giving me the error.


You need to declare it as char *:

void main()
    int select;
    char *filename; // Note the declaration
    select = 0 ;
    filename = "April.txt";

Change it on the parameter declaration for Option_3 too:

void Option_3(char *filename)

The reason is that declaring it as char means you have a variable to hold an object of type char. Character sequences in C are arrays of characters terminated by '\0', thus, char * is the correct type.

UPDATE: Even more appropriate is to declare it as const char *, since you can't modify a string literal.