Search code examples
carraysscanffgetsgetchar

how to store strings inside an array in c


How can I store a list of strings in an array in c. cause usually the a string e.g:'string' is stored as s|t|r|i|g in array(?). now if i have an array that has 10 index and want to store string in each one of those indexes, how do i do it? if not doable, what other data structure can i use for it.

e.g: array = 'string1'|'string2'|..

i have done something, but its not working:

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

EDIT: I realized that array[size] doesn't actually work! I did it cause of my java backgroun d and i thought it might work. so i changed the program but it still is throwing segmentation fault. I post the full(edited) program below:

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

Solution

  • An array of arrays is the way to go.

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