【问题标题】:how to store strings inside an array in c如何在c中将字符串存储在数组中
【发布时间】:2014-10-16 14:53:11
【问题描述】:

如何将字符串列表存储在 c 中的数组中。因为通常a字符串例如:'string'在array(?)中存储为s | t | r | i | g。现在,如果我有一个有 10 个索引的数组,并且想在每个索引中存储字符串,我该怎么做?如果不可行,我可以使用什么其他数据结构。

例如:数组 = 'string1'|'string2'|..

我做了一些事情,但它不起作用:

// Array for name of alphabets, and name of states
 35     char *nameOfStates[numStates];
 36     char buffer[3];
 37 
 38     // Read the nameOfStates 
 39     int i;
 40     for(i=0;i<numStates;i++){
 41         printf("Name of STATES:");
 42 
 43         int z=0;
 44         char *buffer2;
 45         while(z<2 && fgets(buffer,2,stdin) != NULL){
 46             
 47             buffer2 = strndup(buffer,2);
 48             z++;
 49         }// End while-loop
 50         nameOfStates[i] = buffer2;
 51     
 52     }// End of for-loop to read nameOfStates

编辑:我意识到 array[size] 实际上不起作用!我这样做是因为我的 java 背景,我认为它可能会起作用。所以我改变了程序,但它仍然抛出分段错误。我在下面发布完整(编辑的)程序:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

// Function declaration
void analyze(char *string);
void clearNewLines(void);


int main(int argc, char *argv[]){

    // Number of states and number of alphabets of DFA
    int numStates;
    int numAlphabets;


    // Read numStates 
    printf("Enter the number of STATES:");
    scanf("%d",&numStates);

    // Flush STDIN
    clearNewLines();

    // Array for name of alphabets, and name of states
    char **nameOfStates = malloc(numStates*sizeof(char*));
    char *buffer = NULL;    
    // Read the nameOfStates 
    int i;
    for(i=0;i<numStates;i++){   
        printf("Name of STATES:");

        fgets(nameOfStates[i],2*sizeof(char),stdin);    

    }// End of for-loop to read nameOfStates

    clearNewLines();
    // Read numAlphabets
    printf("Enter the number of ALPHABETS: ");
    scanf("%d", &numAlphabets);

    // Flush STDIN
    clearNewLines();

    // Array for name of alphabets, and name of states
    char nameOfAlphabets[numAlphabets]; 
    // Saving transition table
    char *transitionTable[numStates][numAlphabets];


    // Read name of alphabets
    int j;
    for(j=0;j<numAlphabets;j++){

        printf("Name of ALPHABETS:");
        nameOfAlphabets[j] = getchar();

        // Flush STDIN 
        clearNewLines(); 

    }// End for-loop to read alphabets

    // Get the transitionTable[states][alphabets] 
    int row;
    for(row=0;row<numStates;row++){

        int col;
        for(col=0;col<numAlphabets;col++){

            printf("Enter Transition From %s to %c: ",nameOfStates[row],nameOfAlphabets[col]);
            printf("\n");
        }

    }

    return 0;
}// End of main function

/*
*
*   clearNewLines - clear any newline character present at the STDIN
*/
void clearNewLines(void)
{
    int c;
    do
    {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

【问题讨论】:

  • 首先,为指针变量分配内存。
  • 一个数组数组。或char **
  • 数据结构看起来不错(除了你的行缓冲区非常短),但你的循环很奇怪。您从stdin 中读取了i == 0 的所有数据。您只需要一个循环,即外循环。如果输入用完,则中断。 (如果发生这种情况,numStates 当然是错误的。)
  • @SouravGhosh:strndup 分配所需的内存。 buffer 分配在栈上。
  • @MOehm 是的。 buffer2fgets(buffer,2,.. 弄糊涂了。

标签: c arrays scanf fgets getchar


【解决方案1】:

首先:您不能使用变量定义数组大小。我的意思是:char buf[variable]; 不起作用。

你必须这样做:

char **buf;

buf = malloc(sizeof(char) * number_of_strings);
if (buf == NULL)
    return (MALLOC_ERROR);

或者像这样的宏:

// in your header file
#define BUF_SIZE 12
// in your .c file
char *buf[BUF_SIZE];

然后您还必须 malloc 数组的第二维。 例如:

int i;

i = 0
while (buf[i])
{
    buf[i] = malloc(sizeof(char) * string_length);
    if (buf[i] == NULL)
        return (MALLOC_ERROR);
    i++;
}

别忘了释放数组的所有维度。

【讨论】:

  • 我们真的不知道numStates 是什么;它可能是一个预处理器符号。此外,从 C99 开始,还有可变长度数组 (VLA),允许维度为变量。
  • @MOehm 我猜你是对的,但我用 -Wall -Werror -ansi -pedantic 编译
  • 宏需要=吗?没有。
  • @MOehm numStates 是一个整数,从命令行读取
【解决方案2】:

数组数组是要走的路。

//               Array of size 5 (Don't forget to free!)
char **arrayOfStrings = malloc(5*sizeof(char*));
char *aString = "Hi";
arrayOfStrings[0] = aString;
//Literals work too
arrayOfStrings[1] = "Hallo";
aString = "Ahoy";
arrayOfStrings[2] = aString;


ArrayOfStrings values at end: Hi | Hallo | Ahoy | | | 

【讨论】:

  • 这适用于字符串文字,但 OP 从 stdin 读取数据。另外,他的数据结构不是问题,是他的阅读方法。
  • 例如,如果您重复读入同一个缓冲区并分配同一个指针,则不会。如果aString 是一个未初始化的字符串。
  • @MOehm 如果aString 为空,则arrayOfStrings[0] 处的内容为空。否则,如果您使用与缓冲区相同的变量,它将按预期工作。
  • 嗯,有些情况下你必须复制字符串。
  • @MOehm 我觉得我可能误读了这个问题。不过我会留下我的答案,因为有人来这里可能正在寻找这种东西。随意编辑任何警告。
猜你喜欢
  • 1970-01-01
  • 2017-09-06
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多