You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于if条件中可变字符串变量及月份查询程序报错的技术求助

Hey there! Let's break down your two C programming problems step by step—first fixing that annoying 'assignment to expression with array type' error, then tackling how to handle string comparisons in if conditions.

1. Fixing the 'assignment to expression with array type' Error

Why This Happens

In C, an array name acts like a constant pointer to its first element—you can't reassign it directly with the = operator. If you tried something like char month[20]; month = "January";, that's exactly what's triggering the error: you're trying to assign a string literal to an array, which isn't allowed.

Example of the Wrong Code

#include <stdio.h>

int main() {
    int mm;
    char month[20];
    printf("Enter month number: ");
    scanf("%d", &mm);
    
    if (mm == 1) {
        month = "January"; // ❌ This line causes the error!
    }
    printf("Month: %s\n", month);
    return 0;
}

Two Simple Fixes

Fix 1: Use strcpy() (For Arrays)

The strcpy() function from <string.h> lets you copy the contents of a string literal into your array. Just make sure to include the header file:

#include <stdio.h>
#include <string.h> // Required for strcpy()

int main() {
    int mm;
    char month[20];
    printf("Enter month number: ");
    scanf("%d", &mm);
    
    if (mm == 1) {
        strcpy(month, "January"); // ✅ Copy the string into the array
    } else if (mm == 2) {
        strcpy(month, "February");
    } else if (mm == 3) {
        strcpy(month, "March");
    }
    // Add the rest of the months here...
    
    printf("Month: %s\n", month);
    return 0;
}

Fix 2: Use a const char* Pointer (Simpler for This Use Case)

If you don't need to modify the month string later, using a pointer to a string literal works perfectly. Pointers can be reassigned to point to different string literals:

#include <stdio.h>

int main() {
    int mm;
    const char* month; // const because string literals can't be modified
    printf("Enter month number: ");
    scanf("%d", &mm);
    
    switch(mm) { // A switch statement is cleaner here, but if/else works too
        case 1: month = "January"; break;
        case 2: month = "February"; break;
        case 3: month = "March"; break;
        // Add other cases...
        default: month = "Invalid month"; break;
    }
    
    printf("Month: %s\n", month);
    return 0;
}
2. Comparing Variable Strings in if Conditions

Don't Use ==—Here's Why

When you use == with strings, you're comparing the memory addresses of the string variables, not their actual content. Even if two strings have the same text, if they're stored in different parts of memory, == will return false.

Example of the Wrong Approach:

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

int main() {
    char input[20];
    strcpy(input, "January");
    
    if (input == "January") { // ❌ Compares addresses, not content!
        printf("Match!\n");
    } else {
        printf("No match.\n"); // This will always run
    }
    return 0;
}

The Right Way: Use strcmp()

The strcmp() function (also from <string.h>) compares the actual characters of two strings. It returns:

  • 0 if the strings are identical
  • A positive number if the first string is "greater" (lexicographically)
  • A negative number if the second string is "greater"

Example of Correct Comparison:

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

int main() {
    char input_month[20];
    printf("Enter a month name: ");
    scanf("%s", input_month);
    
    if (strcmp(input_month, "January") == 0) { // ✅ Compare content
        printf("That's the first month of the year!\n");
    } else if (strcmp(input_month, "December") == 0) {
        printf("That's the last month of the year!\n");
    } else {
        printf("That's a valid month, but not Jan or Dec.\n");
    }
    return 0;
}

Note for const char* Pointers

Even if you use pointers, relying on == to compare strings is risky. Some compilers optimize identical string literals to share the same address, but this isn't guaranteed across all compilers or scenarios. Always use strcmp() for reliable string comparisons.

内容的提问来源于stack exchange,提问作者S_H_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:39:48