Varying string variable in an if condition

浪尽此生 提交于 2020-01-05 07:50:32

问题


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


回答1:


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.




回答2:


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");
    }



回答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.



来源:https://stackoverflow.com/questions/48734000/varying-string-variable-in-an-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!