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

如何将命令行参数传入C语言函数?CS50作业编译错误求助

Fixing Command Line Argument Passing to Your valid Function in CS50 Substitution

Let's break down the issues in your code and fix them one by one. The core problem here is mismatched parameter types between how you're calling valid and how you've declared it, plus a small syntax error in the function call.

1. Fix the Function Call Syntax

Your current call valid(argc, argv[]); is invalid—you don't need the [] when passing the array. Just pass argv directly, since array names decay to pointers when passed to functions.

Change this line:

valid(argc, argv[]);

To:

valid(argc, argv);

2. Correct the valid Function's Parameter Type

You declared valid to take a string mystring as its second parameter, but argv is an array of strings (aka char** or string argv[]). Using a single string here means you're trying to fit an entire array into a single string variable, which causes all those subscript errors later.

Update the function declaration and definition to accept an array of strings:

Declaration (at the top):

int valid(int argc, string argv[]);

Definition:

int valid(int argc, string argv[]) {
    // ... rest of your code ...
}

3. Fix the Logic Inside valid

Now that argv is correctly passed as an array of strings, your existing logic for checking argv[1] will work properly. The earlier errors about strlen(mystring[1]) and mystring[1][i] were caused because mystring was a single string (not an array), so mystring[1] was trying to access the second character of that single string instead of the second element of the array.

With the corrected parameter type, those lines will behave as intended.

Full Corrected Code

Here's the complete fixed version of your code:

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

// Updated function declaration
int valid(int argc, string argv[]);

int main(int argc, string argv[]) {
    // Fixed function call
    valid(argc, argv);
    return 0; // Don't forget to return from main!
}

// Updated function definition
int valid(int argc, string argv[]) {
    if (argc == 2) {
        int j = 0;
        // Now argv[1] is the command-line key (a string)
        int n = strlen(argv[1]);
        for (int i = 0; i < n; i++) {
            // isalpha returns non-zero if alphabetic, so !isalpha is clearer than checking == false
            if (!isalpha(argv[1][i]))
                j++;
        }
        if (j == 0) {
            if (n == 26) {
                printf("Success.\n");
                return 1;
            } else {
                printf("Key must contain 26 characters.\n");
                return 0;
            }
        } else {
            printf("Usage: ./substitution key\n");
            return 0;
        }
    } else {
        printf("Usage: ./substitution key\n");
        return 0;
    }
}

A Quick Note on isalpha

One small tweak: isalpha returns a non-zero value if the character is alphabetic, not a boolean true. Checking if (isalpha(...) == false) works, but writing if (!isalpha(...)) is more idiomatic C and makes the code easier to read.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:09:45