سؤال

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top