C语言 将用户输入存储到 C 中的字符串数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13080881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing user input into string array in C
提问by Jcmoney1010
I'm writing a program that is supposed to take in a list of names from the user, store them in an array, and then search through the list to check and see if the next name the user enters is part of the original list of names.
我正在编写一个程序,该程序应该接收用户的姓名列表,将它们存储在一个数组中,然后搜索该列表以检查用户输入的下一个姓名是否是原始列表的一部分名称。
The issue I'm having is that when I go to enter a list of names, it only saves the last name entered into the list. Here is the part of code where I have problem:
我遇到的问题是,当我输入姓名列表时,它只保存输入到列表中的姓氏。这是我遇到问题的代码部分:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX_NAMELENGTH 10
#define MAX_NAMES 5
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH]);
int main()
{
char names[MAX_NAMES][MAX_NAMELENGTH];
initialize(names);
getch();
return 0;
}
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH])
{
int i,Number_entrys;
printf("How many names would you like to enter to the list?");
scanf("%d",&Number_entrys);
if (Number_entrys>MAX_NAMES) {
printf("Please choose a smaller entry");
}
else {
for (i=0; i<Number_entrys; i++){
scanf("%s",names[i]);
}
}
printf("%s",names);
}
回答by Anirudh Ramanathan
That should read scanf("%s",names[i]);
那应该读 scanf("%s",names[i]);
Right now, you are storing it as scanf("%s",names);, which is equivalent to scanf("%s",names[0]);
现在,您将其存储为 scanf("%s",names);,这相当于 scanf("%s",names[0]);
So, you are overwriting that same array entry in every pass.
因此,您在每次传递中都覆盖了相同的数组条目。
EDIT:Also, when you pass char names[][]to a function, it only passes the pointer to the first element. You should declare atleast one bound of it, to the same value as the one to which you declared it.
编辑:另外,当你传递char names[][]给一个函数时,它只传递指向第一个元素的指针。您应该至少声明它的一个边界,其值与您声明它的那个边界相同。
int main(){
//To accept 2 names of 2 characters each
char names[2][2];// or char** names;
initialize(names, 2,2);
}
void initialize(char names[][2],const int MAX_NAMES,const int MAX_NAMELENGTH){ .. }
^ syntax error if index not present
(参考)
回答by eyalm
You should store the name into a specific entry in the array:
您应该将名称存储到数组中的特定条目中:
scanf("%s", names[i]);
printf("%s\n", names[i]);
also few generic issues:
还有一些通用问题:
- capital names like MAX_NAMES are used in most cases for definitions and not for variables
- scanf is not a safe function as you cannot limit the amount of chars it reads and writes
- 在大多数情况下,像 MAX_NAMES 这样的大写名称用于定义而不是变量
- scanf 不是一个安全的函数,因为你不能限制它读取和写入的字符数量
回答by paddy
To check your entries so far, you need to loop from 0to i-1(after reading into names[i]) and check each one against the most recent.
到目前为止,要检查您的条目,您需要从0to i-1(在读入 之后names[i])循环并根据最近的条目检查每个条目。
To compare strings you can use strcmp:
要比较字符串,您可以使用strcmp:
if( strcmp(names[i], names[j]) == 0 ) {
/* Duplicate name - reboot universe */
}

