将字符串存储到C中的数组中 [英] Store string into array in c

查看:194
本文介绍了将字符串存储到C中的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我可以创建一个数组,其中包含以下项:

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.

我如何存储输入 HELLO ,以便将其存储到输入[0]中,但现在

How can i store the input "HELLO" such that it stores into input[0] but now

H到输入[0 ],E表示输入[1],依此类推。

H to input[0],E to input[1], and so on.

推荐答案

您需要一个二维字符数组才能包含一个字符串:

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]);
}

这篇关于将字符串存储到C中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆