Search code examples
arrayscvariable-assignmentc-stringsstring-literals

Error in assignment to expression with array type


I am trying to code a program that shows the Easter day when the user inputs the year between 1991 and 2099. All are good but when I am trying to run the program, it says there is an error: assignment to expression with array type. How can I fix this? and what is the wrong with my code?

#include <stdio.h>
#include <stdlib.h>

int main()
{
char month[50];
int year, day;
printf("Please enter a year between 1990 and 2099: ");
scanf("%d", &year);


int a = year-1900;
int b = a%19;
int c = (7*b+1)/19;
int d = (11*b+4-c)%29;
int e = a/4;
int f = (a+e+31-d)%7;
int g = 25 - (d + f);

if (g <= 0 )
{
    month = "March";
    day = 31 + g;
}
else
{
    month = "April";
    day = g;
}
    printf("The date is %s %d", month, day);

}

Solution

  • Arrays do not have the assignment operator. Arrays are non-modifiable lvalues.

    To change the content of an array use for example the standard string function strcpy.

    #include <string.h >
    
    //...
    
    strcpy( month, "March" );
    

    An alternative approach is to declare the variable month as a pointer. For example

    char *month;
    

    or (that is better)

    const char *month;
    

    In this case you may write

    month = "March";