如何在c中的数组内存储字符串

时间:2014-10-16 14:53:12

标签: c arrays scanf fgets getchar

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

例如:array =' 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
编辑:我意识到数组[大小]实际上并不起作用!我做了它导致我的java背景d,我认为它可能会工作。所以我改变了程序,但它仍然在抛出分段错误。我发布下面的完整(编辑)程序:

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

2 个答案:

答案 0 :(得分:3)

首先:您无法使用变量定义数组大小。我的意思是: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++;
}

并且不要忘记释放阵列的所有尺寸。

答案 1 :(得分:1)

可以使用数组数组。

//               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 | | |