如何在 C++ 中通过地址传递将 scanf 字符串传递给全局字符数组?

发布于 2024-12-10 15:29:07 字数 1124 浏览 0 评论 0原文

让我立即澄清这一点,这是大学课程。我不能使用 C++ 库,只能使用标准 C 库。不要建议我使用 C++ 字符串或 cin/cout,因为这对我完成这项作业没有帮助。

我的问题:我在主函数中有全局字符数组。我需要将字符串从函数 foo() 中的 scanf() 传递到全局字符数组。一切都编译得很好,问题是 scanf() 函数似乎对其指向的全局字符数组没有影响。我按照参考书的指示使用“地址”运算符(&)。也许,我不理解字符数组指针和 scanf()“地址”(&) 之间的关系。我觉得我已经到处寻找解决方案。

我在这个问题上花了几个小时,所以我现在正在寻求专家的建议。

这是我的程序的简化版本。

#include <stdio.h>

void foo(char arr1[], char arr2[]);

int main(void)
{
    char arr1[20] = "initial";
    char arr2[25] = "second";

    foo(arr1); // <------- should change it to the string "second" upon scanf()

    printf("Test Return Value: %s\n",arr1); // <---- returns "initial" (the problem)

    printf("Enter a test value: ");
    scanf("%s", &arr1);

    printf("Test Return Value: %s\n",&arr1);

// ---------------------- this code is not part of the issue
fflush(stdin);
getchar();
return 0;
// ----------------------
}
void foo(char arr1[], char arr2[])
{
    // there will be many returned values

    printf("Enter a test value: ");
    scanf("%s", &arr1); // <---------- the problem function (input < 20 chars)
}

Let me make this clear right away, this is for a college class. I cannot use C++ libraries, only standard C libraries. Do not suggest that I use C++ strings or cin/cout because that will not help me for this assignment.

My issue: I have global character arrays in the main function. I need to pass strings to the global character arrays from scanf() in a function foo(). Everything compiles fine, the issue is, the scanf() function seems to have no affect on the global character arrays that it points to. I'm using the "address of" operator (&) as the reference books indicate to do. Perhaps, I'm not understanding the relationship between the character array pointer and the scanf() "address of" (&). I feel I've looked everywhere for a solution.

I've spent several hours on this issue so I'm now looking for expert advice.

Here is a simplified version of my program.

#include <stdio.h>

void foo(char arr1[], char arr2[]);

int main(void)
{
    char arr1[20] = "initial";
    char arr2[25] = "second";

    foo(arr1); // <------- should change it to the string "second" upon scanf()

    printf("Test Return Value: %s\n",arr1); // <---- returns "initial" (the problem)

    printf("Enter a test value: ");
    scanf("%s", &arr1);

    printf("Test Return Value: %s\n",&arr1);

// ---------------------- this code is not part of the issue
fflush(stdin);
getchar();
return 0;
// ----------------------
}
void foo(char arr1[], char arr2[])
{
    // there will be many returned values

    printf("Enter a test value: ");
    scanf("%s", &arr1); // <---------- the problem function (input < 20 chars)
}

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

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

发布评论

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

评论(3

写给空气的情书 2024-12-17 15:29:07
scanf("%s", &arr); // <---------- the problem function (input < 20 chars)

应该是

scanf("%s", arr); // <---------- the problem function (input < 20 chars)

使用C io函数的危险!

scanf("%s", &arr); // <---------- the problem function (input < 20 chars)

should be

scanf("%s", arr); // <---------- the problem function (input < 20 chars)

The perils of using the C io functions!

江湖正好 2024-12-17 15:29:07

尽管您已更新说已解决,但我有一些您可能需要考虑的观察结果:
1、去掉scanfarr1之前的& & printf 调用(这已经解决了 Ayjay 和 Dennis 提到的问题)
2. 未传递给函数 foo 的参数数量正确(如 Adrian Cornish 提到的)。因此代码将无法编译。
3. fflush(stdin); 是未定义的行为。 fflush 仅适用于输出流。请不要将其与 stdin 一起使用。有关详细信息,请参阅此问题
4. 如果这是 C++ 源代码,请使用 #include 而不是 #include
始终使用完整的编译器警告来编译代码并解决所有这些警告。这是一个很好的做法。 :)
希望这有帮助!

Although you have updated saying solved I have a few observation which you might want to consider:
1. Get rid of & before arr1 in scanf & printf calls (which has solved your problem as mentioned by Ayjay & Dennis)
2. Correct number of parameters not passed to function foo (as mentioned by Adrian Cornish). Thus code will not compile.
3. fflush(stdin); is undefined behavior. fflush is only for output streams. Please don't use it with stdin. Refer this SO question for details.
4. If this is a C++ source please use #include <cstdio> instead of #include <stdio.h>
Always compile code with full compiler warnings and resolve all of them. It is a good practice. :)
Hope this helps!

夜无邪 2024-12-17 15:29:07

scanf 函数的正确语法是:

scanf("%s", arr);

您只需要使用 & 运算符来处理简单变量,而不是数组/指针。

除此之外,您还必须纠正 arr1arr2arr 的不当使用。部分代码使用前两个数组,其他部分使用后者。

The correct syntax for the scanf function is:

scanf("%s", arr);

You only need the & operator for simple variables, not for arrays / pointers.

Besides of that, you will have to correct the improper use of arr1, arr2 and arr. Parts of your code make use of the first two arrays, others of the latter.

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