C语言 C - 如何在数组中存储多个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29568297/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:50:53  来源:igfitidea点击:

C - how to store multiple strings in an array

carraysstring

提问by Kyuu

Wondering how store different strings in an array. For example a user would input 'qwe' and the program would then store that in an array variable[0]. Entering another string would then store it as variable[1] and so on

想知道如何在数组中存储不同的字符串。例如,用户将输入“qwe”,然后程序会将其存储在数组变量 [0] 中。输入另一个字符串然后将其存储为变量 [1] 等等

int
main(int argc, char *argv[]) {
    char variable[1000];
    int i;

    printf("enter a variable\n");
    scanf("%s", variable);
    for (i = 0; ??? ;i++) {


        printf("The variable entered was: %s\n",variable[i]);
    }
return 0;

Im new to C so I have no idea what im doing. but thats what I have came up with so far and was wondering if I could get some help with filling in the rest Thanks!

我是 C 新手,所以我不知道我在做什么。但这就是我到目前为止所想出的,我想知道是否可以帮助我填写其余部分 谢谢!

回答by haccks

You can use 2D array to store multiple strings. For 10 strings each of length 100

您可以使用二维数组来存储多个字符串。对于 10 个字符串,每个字符串的长度为 100

char variable[10][100];

printf("Enter Strings\n");
for (int i = 0; i < 10 ;i++)  
    scanf("%100s", variable[i]); 

Better to use fgetsto read string.

最好用于fgets读取字符串。

fgets(variable[i], sizeof(variable[i]), stdin);  

You can also use dynamic memory allocation by using an array of pointers to char.

您还可以通过使用指向 的指针数组来使用动态内存分配char

回答by Lee Daniel Crocker

The most efficient way is to have an array of character pointers and allocate memory for them as needed:

最有效的方法是拥有一个字符指针数组并根据需要为它们分配内存:

char *strings[10];

int main(int ac, char *av[]) {
    memset(strings, 0, 10 * sizeof(char *));

    for (int i = 0; i < 10; i += 1) {
        char ins[100];
        scanf("%100s", ins);

        strings[i] = malloc(strlen(ins) + 1);
        if (strings[i]) {
            strcpy(strings[i], ins);
        }
     }
  }

回答by Nitish Kumar

variable[0]has just stored first letter of string. If you want to store multiple strings in an array you can use 2D array.it has structure like

variable[0]刚刚存储了字符串的第一个字母。如果你想在一个数组中存储多个字符串,你可以使用二维数组。它的结构像

arr[3][100] = { "hello","world", "there"}

and you can access them as

你可以访问它们

printf("%s", arr[0]);one by one.

printf("%s", arr[0]);逐个。

回答by Konstantin Dedov

scanf returns number of successful readed parameters;

scanf 返回成功读取参数的数量;

use 2D array for string-array

使用二维数组作为字符串数组

Never go out of bounds array

永远不要越界数组

#include <stdio.h>

//Use defines or constants!
#define NUM_STRINGS 10
#define MAX_LENGTH_OFSTRING 1000

int main() {
    char variable[NUM_STRINGS][MAX_LENGTH_OFSTRING +1 /*for '##代码##' Null Character */];
    int i = 0;

    printf("enter a variable\n");
    while(scanf("%s", variable[i]) > 0){//if you print Ctrl+Z then program finish work. Do not write more than MAX_LENGTH_OFSTRING symbols
        printf("The variable entered was: %s\n",variable[i]);
        i++;
        if(i >= NUM_STRINGS)
            break;
    }
return 0;
}