【问题标题】:Passing 2D arrays to functions in c将二维数组传递给c中的函数
【发布时间】:2014-12-12 23:14:03
【问题描述】:

我有一个程序,我在其中读取数据,并将它们存储在 1d 和 2d 数组中,然后将它们传递给函数进行分析。我在声明/使用二维数组并将它们传递给函数时遇到问题。

问题: 1)如果我使用 malloc 声明二维数组,那么当我尝试在其中存储数据时,我会在第一行的第二列出现分段错误。但是如果我将其声明为 array[][]

它将正常工作

2)我不能将它传递给函数,以使用它。论坛上有不同的选项,尝试过,但最终都出现错误,说明声明的参数类型和我传递的变量没有相同的数据类型。

完整程序如下:

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

编辑:我根据建议(函数(array[first][second])更改了程序。现在它确实通过了函数但数组为空。

【问题讨论】:

标签: c arrays arguments function-pointers multidimensional-array


【解决方案1】:

将此数组传递给函数:

char transitionTable[numStates][numAlphabets];

函数需要参数:

, size_t numAlphabets, char transitionTable[][numAlphabets],

【讨论】:

    【解决方案2】:

    改变

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

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

    随叫随到

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

    【讨论】:

    • 你应该注意这是一个 C99 特定的解决方案
    • @Cantfindname OP 已使用 C99,请参阅 char transitionTable[numStates][numAlphabets];
    • 对 OP 我强烈建议重构这个函数调用,很难读取这么多参数。
    • @MattMcNabb 我完全同意你的看法!但我是 C 新手,你建议我怎么做?
    • @Deewanagan 创建一个代表当前状态的结构(或者尽可能多的,无论如何)
    【解决方案3】:

    一个简单的解决方案是将二维数组声明为一维数组。 假设您想将一个 10x20 整数数组作为参数传递给函数 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
    

    假设您想访问二维数组的元素 [x,y]。这是通过访问一维数组的元素 x+10*y 来执行的。

    如果数组的大小不是一个常数,那么您也可以将大小作为参数传递,方法是将您的函数更改为:

    void foo(int* a2Darray, int maxX, int maxY)
    

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多