在为输入调用函数时,如何将用户的输入存储到数组中?
How do I store user's input into an array when calling a function for the input?
我是 C 语言的初学者,对于如何通过调用函数进行输入来将输入存储到数组中,我感到非常困惑和无能为力。
我有一个 inputCustomerDetails
函数,但我不太清楚如何将它读取的内容存储到数组中。
typedef struct customer
{
char name[256];
int age;
}Customer
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%s", new_cust.age);
return new_cust;
}
void main(){
Customer customer[5];
Customer *cust;
cust = customer;
*cust = inputCustomerDetails();
for(int i = 0; i < 5; i++)
{
scanf("%d", &cust[i]);
}
}
我认为 scanf
部分显然是错误的,但我基本上是通过阅读数组来反复试验。
您的代码充满错误和误解。
这就是你想要的:
#include <stdio.h>
#include <stdlib.h>
typedef struct customer
{
char name[256];
int age;
} Customer; // a ; was missing here
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // a & was missing here, and %s
// wa used wrongly rather then %d
return new_cust;
}
void main() {
Customer customer[5];
for (int i = 0; i < 5; i++)
{
customer[i] = inputCustomerDetails();
}
// now the customer array contains 5 customers
}
main
函数过于复杂且错误。
您的 inputCustomerDetails
函数填充一个本地 Customer
结构和 returns 一个 copy。您可以将返回的副本 直接 分配给 customer
数组的一个元素。
此外,scanf("%s", new_cust.age);
行是错误的:整数输入使用%d
格式,并将目标整数的地址作为相应的参数传递至 scanf
。 (在读取 .name
成员的情况下,该数组自动 'decays' 进入其第一个元素的地址,这是 %s
说明符所期望的。)
这是一个可能的解决方案:
#include <stdio.h>
typedef struct customer {
char name[256];
int age;
} Customer; // Need a semicolon here!
Customer inputCustomerDetails(void)
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // Note the %d format and the & sign
return new_cust;
}
int main()
{
Customer customer[5];
for (int i = 0; i < 5; i++)
{
customer[i] = inputCustomerDetails();
}
// Check ...
for (int i = 0; i < 5; i++)
{
printf("%s is %d\n", customer[i].name, customer[i].age);
}
}
您的代码中有几点需要解决。 int main(void)
或 int main(int argc, char **argv)
是正确的。除非您有非常具体的原因,否则不要为 main
使用任何其他签名。对于不接受参数的函数,使用显式 void
而不是省略参数列表。现在不是 1987 年。
您必须始终检查由 scanf 编辑的值 return,并且您应该为每次转换使用宽度修饰符。即使是使用原始 %d
的简单代码也可能会在某些输入上表现出未定义的行为(特别是,如果输入字符串不适合整数)。通常,临时代码不会费心限制 %d
转换的输入,但是您应该始终将 %s
的读取限制为不超过缓冲区大小的 1,并且 always 检查 return 值。完全避免 scanf
是最安全的。学习它的弱点不会有启发性,你最终会在愚蠢的错误上浪费大量时间。
不用理会 typedef。它们对于复杂的函数定义和隐藏不透明类型很有用。这些情况都不适用于此处。
按值传递结构可能非常昂贵。不这样做更符合习惯。
#include <stdio.h>
#include <stdlib.h>
struct customer
{
char name[256];
int age;
};
void
inputCustomerDetails(struct customer *new_cust)
{
printf("Enter your name: ");
if( scanf("%255s", new_cust->name) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
printf("Enter your age: ");
if( scanf("%4d", &new_cust->age) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
}
int
main(void)
{
struct customer customer[5];
struct customer *last = customer + sizeof customer / sizeof *customer;
struct customer *cust;
for( cust = customer; cust < last; cust += 1 ){
inputCustomerDetails(cust);
}
return EXIT_SUCCESS;
}
我是 C 语言的初学者,对于如何通过调用函数进行输入来将输入存储到数组中,我感到非常困惑和无能为力。
我有一个 inputCustomerDetails
函数,但我不太清楚如何将它读取的内容存储到数组中。
typedef struct customer
{
char name[256];
int age;
}Customer
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%s", new_cust.age);
return new_cust;
}
void main(){
Customer customer[5];
Customer *cust;
cust = customer;
*cust = inputCustomerDetails();
for(int i = 0; i < 5; i++)
{
scanf("%d", &cust[i]);
}
}
我认为 scanf
部分显然是错误的,但我基本上是通过阅读数组来反复试验。
您的代码充满错误和误解。
这就是你想要的:
#include <stdio.h>
#include <stdlib.h>
typedef struct customer
{
char name[256];
int age;
} Customer; // a ; was missing here
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // a & was missing here, and %s
// wa used wrongly rather then %d
return new_cust;
}
void main() {
Customer customer[5];
for (int i = 0; i < 5; i++)
{
customer[i] = inputCustomerDetails();
}
// now the customer array contains 5 customers
}
main
函数过于复杂且错误。
您的 inputCustomerDetails
函数填充一个本地 Customer
结构和 returns 一个 copy。您可以将返回的副本 直接 分配给 customer
数组的一个元素。
此外,scanf("%s", new_cust.age);
行是错误的:整数输入使用%d
格式,并将目标整数的地址作为相应的参数传递至 scanf
。 (在读取 .name
成员的情况下,该数组自动 'decays' 进入其第一个元素的地址,这是 %s
说明符所期望的。)
这是一个可能的解决方案:
#include <stdio.h>
typedef struct customer {
char name[256];
int age;
} Customer; // Need a semicolon here!
Customer inputCustomerDetails(void)
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // Note the %d format and the & sign
return new_cust;
}
int main()
{
Customer customer[5];
for (int i = 0; i < 5; i++)
{
customer[i] = inputCustomerDetails();
}
// Check ...
for (int i = 0; i < 5; i++)
{
printf("%s is %d\n", customer[i].name, customer[i].age);
}
}
您的代码中有几点需要解决。 int main(void)
或 int main(int argc, char **argv)
是正确的。除非您有非常具体的原因,否则不要为 main
使用任何其他签名。对于不接受参数的函数,使用显式 void
而不是省略参数列表。现在不是 1987 年。
您必须始终检查由 scanf 编辑的值 return,并且您应该为每次转换使用宽度修饰符。即使是使用原始 %d
的简单代码也可能会在某些输入上表现出未定义的行为(特别是,如果输入字符串不适合整数)。通常,临时代码不会费心限制 %d
转换的输入,但是您应该始终将 %s
的读取限制为不超过缓冲区大小的 1,并且 always 检查 return 值。完全避免 scanf
是最安全的。学习它的弱点不会有启发性,你最终会在愚蠢的错误上浪费大量时间。
不用理会 typedef。它们对于复杂的函数定义和隐藏不透明类型很有用。这些情况都不适用于此处。
按值传递结构可能非常昂贵。不这样做更符合习惯。
#include <stdio.h>
#include <stdlib.h>
struct customer
{
char name[256];
int age;
};
void
inputCustomerDetails(struct customer *new_cust)
{
printf("Enter your name: ");
if( scanf("%255s", new_cust->name) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
printf("Enter your age: ");
if( scanf("%4d", &new_cust->age) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
}
int
main(void)
{
struct customer customer[5];
struct customer *last = customer + sizeof customer / sizeof *customer;
struct customer *cust;
for( cust = customer; cust < last; cust += 1 ){
inputCustomerDetails(cust);
}
return EXIT_SUCCESS;
}