存储在char ***时数据损坏

时间:2015-07-12 19:55:21

标签: c

我正在尝试在C中实现DFA (Deterministic Finite Automaton),到目前为止,我尝试将DFA的转换存储在变量transitions中,如图所示以下代码。但由于某些原因,odd transitions索引中的数据已损坏,我认为这是因为我使用了分隔\0的方式。我已将输出附加到代码中,谢谢。

#include<stdio.h>
#include<string.h>
#define MAX_INPUTS 10
#define MAX_INPUT_SIZE 5
#define MAX_STATES 20
#define MAX_STATE_SIZE 5
#define MAX_ID_SIZE 100
int get_id(char str[],char str_arr[][MAX_STATE_SIZE],int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        if(strcmp(str,str_arr[i]) == 0)
            return i;
    }
    return -1;
}
int main()
{
    char inputs[MAX_INPUTS][MAX_INPUT_SIZE];
    char states[MAX_STATES][MAX_STATE_SIZE];
    char*** transitions;
    char temp_state[MAX_STATE_SIZE],temp_input[MAX_INPUT_SIZE],id_string[MAX_ID_SIZE],cur_inp[MAX_INPUT_SIZE],cur_state[MAX_STATE_SIZE];
    int i,j,k,numinps,numstates,row_id,col_id;
    printf("Enter number of input states: ");
    scanf("%d",&numstates);
    printf("Enter number of input symbols: ");
    scanf("%d",&numinps);
    transitions = (char ***) malloc(sizeof(char**)*numstates);
    printf("Enter the input states: \n");
    for(i=0;i<numstates;i++)
    {
        scanf(" %s",&temp_state);
        strcpy(states[i],temp_state);
    }
    printf("Enter the input symbols: \n");
    for(i=0;i<numinps;i++)
    {
        scanf(" %s",temp_input);
        strcpy(inputs[i],temp_input);
    }
    printf("Enter the transitions:\n");
    for(i=0;i<numstates;i++)
    {
        for(j=0;j<numinps;j++)
        {
            transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps);
            transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE);
            printf("\n( %s , %s ) => ",states[i],inputs[j]);
            scanf(" %s",temp_state);
            strcpy(transitions[i][j],temp_state);
        }
    }
    for(i=0;i<numstates;i++)
        for(j=0;j<numinps;j++)
            printf("\n TRANSITION: ( %s , %s) =>  %s\n",states[i],inputs[j],transitions[i][j]);
    printf("Enter a String to find ID: ");
    scanf(" %s",id_string);
    strcpy(cur_state,"q0");
    for(i=0;i<strlen(id_string);i++)
    {
        cur_inp[0] = id_string[i];
        cur_inp[1] = '\0';
        row_id = get_id(cur_inp,inputs,numinps);
        if(row_id == -1)
        {
            printf("Input symbol is not present in sigma");
            exit(0);
        }
        col_id = get_id(cur_state,states,numstates);
        strcpy(cur_state,transitions[row_id][col_id]);
        printf("\n---- %s => %s ----\n",cur_inp,cur_state);
    }
}

OUTPUT

1 个答案:

答案 0 :(得分:4)

这可能是问题所在:

for(i=0;i<numstates;i++)
{
    for(j=0;j<numinps;j++)
    {
        transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps); // <- this line
        transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE);
        printf("\n( %s , %s ) => ",states[i],inputs[j]);
        scanf(" %s",temp_state);
        strcpy(transitions[i][j],temp_state);
    }
}

您在transitions[i]的每个循环中分配j,请改用:

for(i=0;i<numstates;i++)
{
    transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps);
    for(j=0;j<numinps;j++)
    {
        transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE);
        printf("\n( %s , %s ) => ",states[i],inputs[j]);
        scanf(" %s",temp_state);
        strcpy(transitions[i][j],temp_state);
    }
}

j循环中,您会重新定位整个transitions[i],这会导致只存储最后一个字符串