如何在 C++ 中通过地址传递将 scanf 字符串传递给全局字符数组?
让我立即澄清这一点,这是大学课程。我不能使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该是
使用C io函数的危险!
should be
The perils of using the C io functions!
尽管您已更新说已解决,但我有一些您可能需要考虑的观察结果:
1、去掉
scanf
中arr1
之前的&
&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
&
beforearr1
inscanf
&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 withstdin
. 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!
scanf
函数的正确语法是:您只需要使用
&
运算符来处理简单变量,而不是数组/指针。除此之外,您还必须纠正
arr1
、arr2
和arr
的不当使用。部分代码使用前两个数组,其他部分使用后者。The correct syntax for the
scanf
function is: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
andarr
. Parts of your code make use of the first two arrays, others of the latter.