1

I used this program to take input mm as the month of the year and print out the name of the month:

#include <stdio.h>
#include <string.h>
int main(){
int mm;
printf("input month ");
scanf("%d", &mm);
char mname[9];
if (mm == 1) {mname = "January";}
if (mm == 2) {mname = "February";}
if (mm == 3) {mname = "March";}
if (mm == 4) {mname = "April";}
if (mm == 5) {mname = "May";}
if (mm == 6) {mname = "June";}
if (mm == 7) {mname = "July";}
if (mm == 8) {mname = "August";}
if (mm == 9) {mname = "September";}
if (mm == 10) {mname = "October";}
if (mm == 11) {mname = "November";}
if (mm == 12) {mname = "December";}
printf("%d is month %s", mm, mname);

return 0;
}

it gave an error assignment to expression with array type. please help

4
  • 3
    char mname[9]; -> const char *mname; Commented Feb 11, 2018 at 17:04
  • 3
    Or even better: static const char *months[] = { "January", "February", "March", ... }; const char *mname = months[mm - 1]; (sanity check needs to be added). Commented Feb 11, 2018 at 17:07
  • 4
    Even if you could char mname[9] is too short to hold "September" Commented Feb 11, 2018 at 17:09
  • 1
    Or {mname = XXX;} -> {strcpy(mname, XXX;} and mname[9]; -> mname[10]; (you need one char more for the NUL terminator). Commented Feb 11, 2018 at 17:11

3 Answers 3

3

Taking Michael Walz two great comments and adding them as an answer:

#include <stdio.h>
#include <string.h>

void main(int argc, char** argv)
{
    int mm = 0;

    printf("Please enter a month number [1-12]:\n");
    scanf("%d", &mm);
    static const char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

    if (mm >= 1 && mm <= 12)
    {
        printf("%d is month %s", mm, months[mm - 1]);
    }
    else
    {   
        printf("You have entered an invalid month number %d\n", mm);
    }
}

Validity check was done (mentioned in above comments).

Hope it helps.

Cheers,

Guy.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a side note: If you turn somebody's comment into an answer without mentioning their name, nobody will blame you.
3

Basically there are two different ways to think about / talk about strings:

  1. An array of characters, terminated by a '\0' character. (This is the formal definition of a string in C.)

  2. As a pointer to character, or char *, pointing at the first of a sequence (an array) of characters, terminated by a '\0' character.

So you can declare an array, and copy a string into it:

char arraystring[10];
strcpy(arraystring, "Hello");

Or you can declare an array, and give it an initial value when you declare it:

char arraystring2[] = "world!";

Or you can declare a pointer, and make it point to a string:

char *pointerstring;
pointerstring = "Goodbye";

Or you can declare a pointer, and give it an initial value:

char *pointerstring2 = "for now";

It's worth knowing how these "look" in memory:

                +---+---+---+---+---+---+---+---+---+---+
arraystring:    | H | e | l | l | o |\0 |\0 |\0 |\0 |\0 |
                +---+---+---+---+---+---+---+---+---+---+

                +---+---+---+---+---+---+---+
arraystring2:   | w | o | r | l | d | ! |\0 |
                +---+---+---+---+---+---+---+

                +---------------+
pointerstring:  |       *       |
                +-------|-------+
                        |           +---+---+---+---+---+---+---+---+
                        +---------> | G | o | o | d | b | y | e |\0 |
                                    +---+---+---+---+---+---+---+---+

                +---------------+      
pointerstring2: |       *       |
                +-------|-------+
                        |           +---+---+---+---+---+---+---+---+
                        +---------> | f | o | r |   | n | o | w |\0 |
                                    +---+---+---+---+---+---+---+---+

Now, the thing is, you can't assign arrays in C. You can assign pointers. You can also make use of the special rule (the "equivalence between arrays and pointers") by which when you use an array in an expression, what you get is a pointer to the array's first element.

So if you want to assign one string-as-pointer to another string-as-pointer, that works:

pointerstring = pointerstring2;

If you try to assign one string-as-array to another string-as-array, that doesn't work

arraystring = arraystring2;     /* WRONG -- compiler complains, attempt to assign array */

If you want to copy one string-as-array to another, you have to call strcpy (and of course you have to worry about overflow):

strcpy(arraystring, arraystring2);

You can also assign a string-as-array to a string-as-pointer:

 pointerstring = arraystring;

This works because the compiler treats it exactly as if you'd written

pointerstring = &arraystring[0];

Finally, if you attempt to assign a string-as-pointer to a string-as-array, this doesn't work, again because you can't assign to an array:

 arraystring = pointerstring;    /* WRONG */

Again, you could call strcpy instead, as long as you're sure the string will fit:

strcpy(arraystring, pointerstring);  /* but worry about overflow */

In your original code, mname was a string-as-array, so you can't assign to it. You have two choices:

  1. Use strcpy to copy strings into it:

    if (mm == 1) { strcpy(mname, "January"); }

  2. Declare mname as p a pointer instead:

    char *mname;

    ...

    if (mm == 1) { mname = "January"; }


Addendum: For completeness, I should mention one more set of points.

When you initialize a pointer to point to a string, in either of these ways:

char *pointerstring = "Goodbye";
char * pointerstring2;
pointerstring2 = "for now";

those strings "Goodbye" and "for now" are read-only. You can't modify them. So if you try to do something like

 strcpy(pointerstring, pointerstring2);    /* WRONG: overwriting constant string */

it won't work, because you're trying to copy the second string into the memory where the first string is stored, but that memory isn't writable.

So when you're using arrays, you can't use assignment, you must use strcpy; but when you're using pointers, you can use assignment, and you probably can't call strcpy.

Comments

1

Basically array types are constant pointers, so when you try to assign a new value to pointer mname the compiler detects an error.

You could use function strcpy as in the following example to solve the problem:

    if (mm == 1) {
        strcpy(mname, "January");
    }

2 Comments

Basically array types are constant pointers Well, no. Arrays are not pointers. I know what you're trying to say there, and it's a convenient and once-popular simplification, but experience has shown that it confuses people more than it helps them.
Arrays are "constant" only in that you can't assign to them with =. The true relationship between arrays and pointers is that when you mention an array in an expression, as if to use its value, the value you get is a pointer to the array's first element. That doesn't mean that the array was a pointer, just that it "decayed" to one upon use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.