1

I have a program where i read data, and store them in 1d and 2d arrays, and then pass them to a function to analyze it. I have problem with declaring/using 2d arrays and passing them to function.

PROBLEMS: 1) if i use malloc to declare 2d array, then i get segmentation fault on the second col of first row when trying to store data in it. but it will work fine if i declare it as array[][]

2)i can't pass it to function, to use it. there were different options here on the forum, tried them but it all ends up with errors stating the declared argument type and the variable i am passing doesn't have same data type.

Full program below:

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

// Function declaration
void clearNewLines(void);
int match(int numStates, int numAlphabets, char *nameOfStates, char *nameOfAlphabets, char finalState,char *transitionTable, int stringLength,char *string);

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

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

    // Language string
    char *string;
    int stringLength;

    // Final state
    char finalState;

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

    // Read the nameOfStates 
    int i;
    for(i=0;i<numStates;i++){

        if(i==0)
            printf("Enter name of states as numbers(1-9)\n");   

        printf("Name of STATES:");
        scanf("%c",&nameOfStates[i]);
        clearNewLines();

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

    }// End of for-loop to read nameOfStates

    // 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];   
    char *nameOfAlphabets = malloc(numAlphabets * sizeof(char));
    // Saving transition table
    //char **transitionTable = malloc(sizeof(char*) * numStates * numAlphabets);
    char transitionTable[numStates][numAlphabets];

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

        // Read the alphabets
        printf("Name of ALPHABETS:");
        scanf("%c",&nameOfAlphabets[j]);

        // 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 q%c to %c: ",nameOfStates[row],nameOfAlphabets[col]);
            scanf("%c",&transitionTable[row][col]);
            clearNewLines();
        }

    }// End of (outer) for-loop to store data in transition table

    // Get final state
    printf("Enter final state: ");
    scanf("%c",&finalState);
    clearNewLines(); 

    // Get language string
    printf("Enter the length of string: ");
    scanf("%d",&stringLength);
    clearNewLines();
    string = malloc(stringLength*sizeof(char));
    printf("Enter string: ");
    scanf("%s",string);
    clearNewLines();

    int result =  match(numStates, numAlphabets, nameOfStates, nameOfAlphabets, finalState, (char*)transitionTable, stringLength, string);


    return result;


}// End of main function


/*
*
*   match - check if a string matches a language
*/

int match(int numStates, int numAlphabets, char *nameOfStates, char *nameOfAlphabets, char finalState,char *transitionTable, int stringLength,char *string){


    char state;     // State of the machine
    char stringChar;// Character of string being processed
    int result;     // Result of the character processing

    result = 0;
    int i = 0;
    // initial state
    state = nameOfStates[0];
    stringChar = string[i];

    // Walk through the string, while doing the transition
    while(i < stringLength){

        int row;
        for(row=0;row<numStates;row++){

            if(state == nameOfStates[i]){
                break;
            }           
        }// End of for-loop to find the state

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

            if(stringChar == nameOfAlphabets[col]){
                break;
            }
        }// End of for-loop to find the alphabet

        state = transitionTable[row][col];
        // Next character   
        i++;
        stringChar = string[i];

    }// End of while-loop to go thorough the string characters of the language    

    // If in final state, then accepted, if not then rejected
    if(state == finalState){

        result = 1;
    }else{

        result = 0;
    }

    return result;

}// End of match function

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

EDIT: I changed the program based on a suggestion ( function(array[first][second] ). Now it does pass the function but the array is empty.

aaa
  • 425
  • 6
  • 21
  • Note that strings are null terminated in C and you need to allocate space for the null termination character. – Lundin Oct 17 '14 at 11:48
  • Possible duplicate: [How to pass a multidimensional array to a function in C and C++](http://stackoverflow.com/questions/2828648/how-to-pass-a-multidimensional-array-to-a-function-in-c-and-c) – legends2k Oct 17 '14 at 13:21
  • Closely related: [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function/17569578) – legends2k Oct 17 '14 at 13:23

3 Answers3

1

An easy solution is to declare your 2D array as an 1D array. Let's say you want to pass as an argument a 10x20 array of integers to function foo:

void foo(int* a2Darray){
    ...
}

int* my2Darray = malloc(10*20*sizeof(*my2Darray)); //allocate your array as an 1D array
foo(my2Darray); //pass it as an argument

Then let's say you want to access the element [x,y] of your 2D array. This is performed by accessing the element x+10*y of the 1D array.

If the size of the array is not a constant, then you can also pass the size as arguments, by changing your function to :

void foo(int* a2Darray, int maxX, int maxY)
Cantfindname
  • 1,708
  • 1
  • 12
  • 28
0

change

int match(int numStates, int numAlphabets, char *nameOfStates, char *nameOfAlphabets, char finalState,char *transitionTable, int stringLength,char *string){

to

int match(int numStates, int numAlphabets, char *nameOfStates, char *nameOfAlphabets, char finalState,char transitionTable[numStates][numAlphabets], int stringLength,char *string){

at call

int result =  match(numStates, numAlphabets, nameOfStates, nameOfAlphabets, finalState, transitionTable, stringLength, string);
BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68
0

To pass this array to a function:

char transitionTable[numStates][numAlphabets];

the function needs parameters:

, size_t numAlphabets, char transitionTable[][numAlphabets],
M.M
  • 130,300
  • 18
  • 171
  • 314