编写我的第一个 C 程序,但我无法克服这个愚蠢的错误
我使用 xCode 是因为我发现调试器非常有用。所以在调试器中我看到,在我输入学生姓名 name[0] = \0 后,无论如何。但名称的其余部分将是正确的。例如,如果我输入 John,它会返回 \0、o、h、n。请帮忙?
char name[MAX_NAME_LENGTH];
char department[MAX_DEPT_LENGTH];
int rank;
int empty = 0;
printf("\nPlease enter the students name: ");
scanf("%s", &name);
printf("\nPlease enter the students department: ");
scanf("%s", &department);
printf("\nPlease enter the students rank: ");
scanf("%d", &rank);
strcpy(studentArray[empty].name, name);
strcpy(studentArray[empty].department, department);
studentArray[empty].rank = rank;
I'm using xCode because I found the debugger to be very useful. So in the debugger I see, after I enter the students name name[0] = \0 no matter what. But then the rest of the name will be correct. For example, if I put John, it will come back saying \0, o, h, n. Help please?
char name[MAX_NAME_LENGTH];
char department[MAX_DEPT_LENGTH];
int rank;
int empty = 0;
printf("\nPlease enter the students name: ");
scanf("%s", &name);
printf("\nPlease enter the students department: ");
scanf("%s", &department);
printf("\nPlease enter the students rank: ");
scanf("%d", &rank);
strcpy(studentArray[empty].name, name);
strcpy(studentArray[empty].department, department);
studentArray[empty].rank = rank;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
执行此操作:
请注意对
scanf
的前两次调用中没有 & 符号。这是因为编译器隐式地将name
和department
转换为指向各自数组第一个元素的指针 (&name[0]
, < code>&department[0]) 当它们在表达式中使用时(对此规则有期望,请参阅 此处了解详细信息)。请阅读本文以获取更多参考。
Do this:
Note the absence of ampersands in the first two calls to
scanf
. This is because the compiler implicitly convertsname
anddepartment
into pointers to the first elements of the respective arrays (&name[0]
,&department[0]
) when they are used in an expression (there are expections to this rule, see here for details).Read this for further reference.
您需要使用
scanf("%s", name);
而不是scanf("%s", &name);
(与department< /代码>)。
scanf
需要一个要写入的内存地址。对于字符串,它需要一个char *
并传递一个char[]
在这种情况下就可以了(但它很容易受到缓冲区溢出的影响!)。但是,对于您的整数(不是指针),您需要传递整数的内存地址,即
&rank
而不是rank
> - 你已经这样做了。You need to use
scanf("%s", name);
instead ofscanf("%s", &name);
(same fordepartment
).scanf
needs a memory address to write to. In case of a string it expects achar *
and passing achar[]
is fine in this case (it is vulnerable to buffer overflows though!).However, for your integer - which is not a pointer - you need to pass the memory address of the integer, i.e.
&rank
instead ofrank
- which you already did.scanf 函数需要知道它需要读入的内存中的地址,因此对于整数之类的东西
是正确的。但是像 name 这样的东西已经是地址(数组的名称实际上是其第一个元素的地址),所以
你想要的不是 :
,对于其他数组也类似。
The scanf function needs to know the address in memory it needs to read in, so for things like integers
is correct. But things like name are already addresses (the name of an array is effectively the address of its first element) so instead of :
you want:
And similarly for other arrays.
当你声明一个数组时;
没有索引的
name
是指向数组中第一个元素的指针。当函数(例如
scanf()
)调用char *
时,它需要指向第一个元素的指针。When you declare an array;
name
without an index is a pointer to the first element in the array.When a function (such as
scanf()
) calls for achar *
it wants the pointer to the first element.您的变量
name
和department
是字符数组 (char (*)[]
)。当您将这些变量传递给 printf/scanf (或任何其他函数,数组通过引用传递)时,它们的类型衰减为TYPE*
(在您的情况下char*
)并且作为数组中第一个元素的内存位置传递。当您尝试传递变量的地址 (
&name
) 时,您将传递一个指向该变量的内存位置的指针(在您的情况下,这会衰减为char(*)[0 ]
)。这个问题引起了我的兴趣,因为name
和&name
将具有相同的值,但类型不同。由于 printf/scanf 被定义为接收 char*,传递 char(*)[] 会导致未定义的行为 - 一些编译器会为您处理 if,而其他编译器则不会(VS2010 和早期版本可能会处理此问题)为你)。我很接近,但对这个答案有一些帮助(学到了很多): & C 中数组的运算符定义。如果他们在这里提供答案,我会删除我的。
Your variables
name
anddepartment
are character arrays (char (*)[]
). When you pass these variables to printf/scanf (or any other function, arrays are passed by reference), their types decay toTYPE*
(in your casechar*
) and are passed as the memory location of the first element in the array.When you try to pass the address of the varible (
&name
), you are passing a pointer to that variable's memory location (in your case, this decays tochar(*)[0]
). This question intrigued me becausename
and&name
will have the same value, but different types. Since printf/scanf are defined to receivechar*
, passing char(*)[] results in undefined behavior - some compilers will handle if for you while others won't (VS2010 and maybe earlier versions handle this for you).I was close, but had some help with this answer (learned a lot): & operator definition for arrays in C. If they provide an answer here, I'll delete mine.
使用
scanf("%s", name);
避免使用 &,太容易出错。
use
scanf("%s", name);
avoid using &, it's too easy to get wrong.