C语言 将字符串存储到c中的数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21376645/
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
Store string into array in c
提问by MoonScythe
As i know, i can create an array with item inside such as:
据我所知,我可以创建一个包含项目的数组,例如:
char *test1[3]= {"arrtest","ao", "123"};
but how can i store my input into array like code above because i only can code it as
但是如何将我的输入存储到数组中,如上面的代码,因为我只能将其编码为
input[10];
scanf("%s",&input) or gets(input);
and it store each char into each space.
并将每个字符存储到每个空间中。
How can i store the input "HELLO"such that it stores into input[0] but now
我如何存储输入“HELLO”以便它存储到 input[0] 但现在
H to input[0],E to input[1], and so on.
H 输入[0],E 输入[1],依此类推。
采纳答案by cybermage14
You need a 2 dimensional character array to have an array of strings:
您需要一个二维字符数组来拥有一个字符串数组:
#include <stdio.h>
int main()
{
char strings[3][256];
scanf("%s %s %s", strings[0], strings[1], strings[2]);
printf("%s\n%s\n%s\n", strings[0], strings[1], strings[2]);
}
回答by Don't You Worry Child
Use a 2-dimensional array char input[3][10];
or
an array of charpointers (like char *input[3];) which should be allocated memory dynamically before any value is saved at those locations.
使用二维数组char input[3][10];
或指针
数组char(如char *input[3];),在这些位置保存任何值之前,应动态分配内存。
First Case, take input values as scanf("%s", input[0]);, similarly for input[1]and input[2]. Remember you can store a string of max size 10(including '\0'character) in each input[i].
第一种情况,将输入值作为scanf("%s", input[0]);,input[1]与 和类似input[2]。记住,你可以存储最大尺寸的字符串10(包括'\0'字符)中的每个input[i]。
In second case, get input the same way as above, but allocate memory to each pointer input[i]using mallocbefore. Here you have flexibility of size for each string.
在第二种情况下,以与上述相同的方式获取输入,但为input[i]使用mallocbefore 的每个指针分配内存。在这里,您可以灵活地调整每个字符串的大小。
回答by Vijay
Did not really understand what you need. But here is what I guessed.
没有真正理解你需要什么。但这是我的猜测。
char *a[5];//array of five pointers
for(i=0;i<5;i++)// iterate the number of pointer times in the array
{
char input[10];// a local array variable
a[i]=malloc(10*sizeof(char)); //allocate memory for each pointer in the array here
scanf("%s",input);//take the input from stdin
strcpy(a[i],input);//store the value in one of the pointer in the pointer array
}
回答by dead programmer
try below code:
试试下面的代码:
char *input[10];
input[0]=(char*)malloc(25);//mention the size you need..
scanf("%s",input[0]);
printf("%s",input[0]);
回答by Kshitij Patidar
int main()
{
int n,j;
cin>>n;
char a[100][100];
for(int i=1;i<=n;i++){
j=1;
while(a[i][j]!=EOF){
a[i][j]=getchar();
j++;
}
}
回答by angelita
This code inspired me on how to get my user input strings into an array. I'm new to C and to this board, my apologies if I'm not following some rules on how to post a comment. I'm trying to figure things out.
这段代码启发了我如何将我的用户输入字符串放入数组中。我是 C 语言和这个董事会的新手,如果我没有遵守有关如何发表评论的一些规则,我深表歉意。我正在努力解决问题。
#include <stdio.h>
int main()
{
char strings[3][256];
scanf("%s %s %s", strings[0], strings[1], strings[2]);
printf("%s\n%s\n%s\n", strings[0], strings[1], strings[2]);
}

