简体   繁体   中英

storing string and characters in two dimensional array (C)

I am trying to write a program to simulate a DFA. what I need to do is to take a number of input from the user and save it in two independent arrays(use it as row and columns), and then create a third array(2d) that acts as a table of values for the first two arrays.

eg: array2 = {a, b} array1 ={q1,q2,q3} array[array1][array2] = (TABLE BELOW)

  a   b 

========

q1| v1 v2

q2| v3 v4

q3| v5 v6

PROBLEMS:

1) I can't save the strings q1,q2,q3... in array

2) the second array values somehow overwrite the first array values,(maybe because i am using the same variable as their counter? if i change the counter variable for the second loop, it gives segmentation fault

It would be great if someone could point out which part I am doing wrong.

EDIT: the segmentation problem was solved, thanks to answers by coolguy and jayesh. I still have one problem, its that the array1 doesn't return a string, it returns only character, if i enter q1 it returns q only.

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

// Function declaration
void clearNewLines(void);


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

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

    // Array for name of alphabets, and name of states
    char nameOfAlphabets[numAlphabets]; 
    char nameOfStates[numStates];

    // Saving transition table
    char *transitionTable[numStates][numAlphabets];

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

    // Flush STDIN
    clearNewLines();

    // Read the nameOfStates 
    int i;
    for(i=0;i<numStates;i++){   
        printf("Name of STATES:");
        fgets(&nameOfStates[i], 100,stdin);
    }// End of for-loop to read nameOfStates

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

    // Flush STDIN
    clearNewLines();

    // Read name of alphabets

    for(i=0;i<numAlphabets;i++){

        printf("Name of ALPHABETS:");
        nameOfAlphabets[i] = 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 %c 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);
}
int numStates;
int numAlphabets;

char nameOfAlphabets[numAlphabets]; 
char nameOfStates[numStates];
char *transitionTable[numStates][numAlphabets];

Here numAlphabets and numStates are uninitialized.

Move definition after the scanf .

Like

    int numStates;
    int numAlphabets;

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

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

    char nameOfAlphabets[numAlphabets]; 
    char nameOfStates[numStates];
    char *transitionTable[numStates][numAlphabets];
 int numStates;
 int numAlphabets;
 char nameOfAlphabets[numAlphabets];
 char nameOfStates[numStates];  

Here,you are making arrays of uninitialised int s which cause the segmentation fault. Move the array initializations after the respective scanf s so that the two int s get initialized before making the array.

char *transitionTable[numStates] [numAlphabets];

Move the above line after the two scanf s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM