简体   繁体   English

C - 结构中的结构数组在 dfa 程序中返回意外结果

[英]C - Array of structs within struct returning unexpected results in dfa program

I am trying to make a program that creates a dfa based on user input.我正在尝试制作一个基于用户输入创建 dfa 的程序。 Each node and transition must be a struct and the dfa is also a struct.每个节点和过渡必须是一个结构体,而 dfa 也是一个结构体。 The dfa struct contains an array of node structs and a linked list of transition structs. dfa 结构体包含一个节点结构体数组和一个转换结构体链表。 Here is what I have so far:这是我到目前为止所拥有的:

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

#define MAX_STATES 25
#define TRUE 1
#define FALSE 0
#define MAXCHAR 256

/* struct for a state of the DFA */
typedef struct state {
    char *id;
    int isInitial;
    int isFinal;
} STATE_T;

/* struct for a transition of the DFA*/
typedef struct transition {
    STATE_T *startState;
    char condition;
    STATE_T *endState;
    struct transistion *next;
} TRANSITION_T;

/* struct for the DFA */
typedef struct automaton {
    STATE_T state[MAX_STATES];
    TRANSITION_T *transition;
} AUTOMATON_T;

/* main function reads initial user inputs to build dfa */
int main() {
    AUTOMATON_T dfa;
    STATE_T stateA;
    TRANSITION_T *trans;

The first line of the text file is comma sperated nodes/states, for example: "a,b,c".文本文件的第一行是逗号分隔的节点/状态,例如:“a,b,c”。 But when I get to assigning each node as a state id into the dfa struct, the final state is assigned to the id for every single state.但是当我开始将每个节点作为状态 id 分配到 dfa 结构中时,最终状态会分配给每个状态的 id。 The for statement in the following code prints 0c 1c 2c where I would expect 0a 1b 2c.以下代码中的 for 语句打印 0c 1c 2c,而我期望的是 0a 1b 2c。

    FILE *fp = fopen(fileName, "r");

    while (fgets(useIn, MAXCHAR, fp)) {
        if (lineNum == 0) {
            i = 0;
            char *token = strtok(useIn, ",");
            while (token != NULL) {
                strcpy(stateId, token);
                stateA.id = stateId;
                dfa.state[i] = stateA;
                i++;
                token = strtok(NULL, ",");
            }
            stateAmt = i;
            for (i = 0; i < stateAmt; i++) {
                printf("%d", i);
                printf("%s", dfa.state[i].id);
            }
        }
    fclose(fp);
    return 0;
}

Why would this be?为什么会这样? Am I handling the array of states incorrectly?我是否错误地处理了状态数组?

The issue is these two lines:问题是这两行:

strcpy(stateId, token);
stateA.id = stateId;

In the state structure, the id member is a pointer to a mutable string.state结构中, id成员是一个指向可变字符串的指针。 The memory for storing that string was only allocated once, and its address has been stored in each state struct.用于存储该字符串的内存只分配了一次,其地址已存储在每个状态结构中。

In order for each state to have its own ID, you need to allocate a new ID buffer for each state.为了让每个状态都有自己的 ID,您需要为每个状态分配一个新的 ID 缓冲区。 You could do this dynamically using malloc at run-time, or you could simply include the storage for this buffer as part of the state structure like this:您可以在运行时使用malloc动态执行此操作,或者您可以简单地将此缓冲区的存储包含在state结构中,如下所示:

typedef struct state {
    char id[MAX_ID_LENGTH+1];
    int isInitial;
    int isFinal;
} STATE_T;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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