C 参数字符数组

发布于 2025-01-03 18:56:36 字数 454 浏览 0 评论 0原文

我想传递参数并将它们输出为字符或字符串数​​组。做到这一点最简单的方法是什么?一旦我将它们作为字符数组,我希望能够操作该数组。我能够传递它们并使用字符指针遍历字符串,但我想使用字符数组。有什么简单的方法可以做到这一点吗?我对 C 还很陌生,这是我到目前为止所掌握的。

    if(argc != 3){
            printf("incorrect number of arguments, program needs 3\n");
            exit(1);
    }

    char s1[20];
    s1 = (char*)argv[1];
    char s2[20];
    s2 = (char*)argv[2];

    printf("String1 is %s\n", s1);
    printf("String2 is %s\n", s2);

    exit(0);

I would like to pass in arguments and output them as an array of characters or string. What is the easiest way to do this? Once I have them as an array of characters I want to be able to manipulate the array. I was able to pass them in and use a character pointer to traverse through the string but I'd like to use a char array. Is there any easy way to do this? Im pretty new to C, here is what I have so far.

    if(argc != 3){
            printf("incorrect number of arguments, program needs 3\n");
            exit(1);
    }

    char s1[20];
    s1 = (char*)argv[1];
    char s2[20];
    s2 = (char*)argv[2];

    printf("String1 is %s\n", s1);
    printf("String2 is %s\n", s2);

    exit(0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有心的人 2025-01-10 18:56:37

argv 数组指向的字符串是可以修改的,你可以只修改argv 数组指向的字符串,不需要复制它们。

从C标准来看:

(C99, 5.1.2.2.1p2) “参数 argc 和 argv 以及 argv 数组指向的字符串应可由程序修改,并在程序启动和程序终止之间保留其最后存储的值。”< /p>

The strings pointed to by the argv array are modifiable, you can just modify the strings pointed to by the argv array, there is no need to copy them.

From the C standard:

(C99, 5.1.2.2.1p2) "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination."

黯然 2025-01-10 18:56:37

您可以直接直接打印 argv 的值:

printf("String1 is %s\n", argv[1]);
printf("String2 is %s\n", argv[2]);

如果您需要修改字符串,请按以下方法操作

char *newString1 = malloc(strlen(argv[1]) + 1 +  charactersToAdd); // add one for null termination.
strcpy(newString1, argv[1]);

// manipulate newString1 however you choose
printf("String1 is %s\n", newString1);
free(newString1);

You can simply print the values of argv directly:

printf("String1 is %s\n", argv[1]);
printf("String2 is %s\n", argv[2]);

If you need to modify the strings, here is how you would do that

char *newString1 = malloc(strlen(argv[1]) + 1 +  charactersToAdd); // add one for null termination.
strcpy(newString1, argv[1]);

// manipulate newString1 however you choose
printf("String1 is %s\n", newString1);
free(newString1);
潇烟暮雨 2025-01-10 18:56:37

argv[1]argv[2] 已经是指向字符数组的指针,因此您可以直接使用它们。此外(请参阅@ouah 的帖子),字符串已经是可变的,因此您可能根本不需要除此之外的任何内容。

如果您确实想要一个作用域本地数组,则需要使用称为“可变长度数组”的 C99 功能,因为字符串长度仅在运行时已知:

#include <string.h>

int main(int argc, char * argv[])
{
    char s1[strlen(argv[1]) + 1];
    strncpy(s1, argv[1], sizeof s1);

    // ...
}

或者您可以malloc 足够的空间用于字符串的副本(例如 char * s1 = malloc(strlen(argv[1]) + 1);),但是这样你就没有本地毕竟数组(你可能会这样我们将使用原始字符串,除非您真的想复制)。

argv[1] and argv[2] are already pointers to character arrays, so you can use those directly. Moreover (see @ouah's post), the strings are already mutable, so you might not need anything beyond this at all.

If you really want to have a scope-local array, you need to use the C99 feature called "variable-length arrays", since the string lengths are only known at runtime:

#include <string.h>

int main(int argc, char * argv[])
{
    char s1[strlen(argv[1]) + 1];
    strncpy(s1, argv[1], sizeof s1);

    // ...
}

Alternatively you could malloc enough space for a copy of the string (say char * s1 = malloc(strlen(argv[1]) + 1);), but then you wouldn't have a local array after all (and you might as well use the original strings, unless you really wanted to make a copy).

旧时模样 2025-01-10 18:56:37

argv 已经是 char* (实际上是数组的数组),但是,它们被标记为 const,如“不要修改”;) 。你想要的实际上是复制它们,这样你就可以自由地操纵它们:

char str[40];
strcpy (str,argv[1]);

现在你可以按照你的意愿操纵 str 。

argv is already char* (actually an array of an array), however, they are noted const as in "do not modify" ;). What you want is actually making a copy of them so you can manipulate them freely:

char str[40];
strcpy (str,argv[1]);

Now you can manipulate str as you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文