Home Varying string variable in an if condition
Reply: 3

Varying string variable in an if condition

Sayalkumar Hajare
1#
Sayalkumar Hajare Published in 2018-02-11 16:57:40Z

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

angelvmx
2#
angelvmx Reply to 2018-02-11 17:12:29Z

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");
    }
Guy Avraham
3#
Guy Avraham Reply to 2018-02-11 17:29:05Z

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.

Steve Summit
4#
Steve Summit Reply to 2018-02-11 17:58:13Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300142 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO