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

C语言实现维吉尼亚密码时的main参数错误及密钥传参求助

Fixing Your Vigenère Cipher Command-Line Argument Errors

Let's walk through fixing those two compilation errors so you can pass your encryption key via command-line arguments properly.

Error 1: Incorrect main Function Parameter Type

vigenere.c:7:5: error: second parameter of 'main' (argument array) must be of type 'char **'
int main(int argc, char argv[])
^

The C standard requires the second parameter of main to be either a pointer to an array of character pointers (char **argv) or an array of character pointers (char *argv[])—these two are functionally equivalent. Your code uses char argv[], which declares a single array of characters (not an array of pointers to strings), which is invalid for handling command-line arguments.

Fix: Update your main function signature to either:

int main(int argc, char *argv[])

or

int main(int argc, char **argv)

Error 2: Mismatched Type for the Key Variable

vigenere.c:10:15: error: incompatible integer to pointer conversion initializing 'char ' with an expression of type 'char'; take the address with & [-Werror,-Wint-conversion]
char key = argv[1];
^ ~~~~~~~ &

argv[1] is a char *—a pointer to the first command-line argument string (your encryption key). You're trying to assign this pointer to a single char variable, which is a critical type mismatch. You need a pointer to hold the entire key string.

Fix: Declare key as a char * instead of char:

char *key = argv[1];

Bonus: Add Safety Checks

It's also smart to add safeguards to ensure the user provides a key, and that the key only contains valid alphabetic characters (since Vigenère Cipher relies on letters). Here's a complete corrected snippet with these checks:

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

int main(int argc, char *argv[]) {
    // Check if exactly one key argument was provided
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <encryption-key>\n", argv[0]);
        return 1;
    }

    char *key = argv[1];
    // Validate the key contains only alphabetic characters
    for (int i = 0; key[i] != '\0'; i++) {
        if (!isalpha(key[i])) {
            fprintf(stderr, "Error: Key must only contain letters (A-Z, a-z)\n");
            return 1;
        }
        // Normalize key to uppercase (or lowercase) for consistent encryption logic
        key[i] = toupper(key[i]);
    }

    // Insert your Vigenère encryption/decryption logic here
    printf("Successfully loaded key: %s\n", key);
    return 0;
}

This code will compile cleanly, validate user input, and properly capture your encryption key from the command line.

内容的提问来源于stack exchange,提问作者Helder Gonçalves

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:27:06