我是一个初学者(用C语言)试图编写一个简单的自动机。我有一个包含50个指向lTransitions类型结构的指针的数组automaton[]。我想调用addTransition(&automaton, s1, &t)将一个转换添加到列表中(t指向刚刚用malloc()创建的结构...)。如果automaton[state1]是NULL,那么我需要用t指向的地址替换它。否则,我需要遵循链,直到automaton[state1]->next为NULL。
问题是测试总是返回false,因为*(automaton+e1)是指针的地址,而不是它应该指向的结构(如果没有指针,则返回NULL )。
任何帮助都将不胜感激。
以下是我的代码的关键行:
struct lTransitions { char c;
int stateNext;
struct lTransition *next };
struct lTransitions *automaton[50]=NULL;
void addTransition( struct lTransition **automaton, int state1, struct lTransition *t){
...
if (*(automaton+e1)==NULL) { *(automaton+e1) = t; }
else { ... }发布于 2017-11-05 23:44:11
因为你传递的是整个数组的地址,而不是数组,所以你不能访问它的元素,在你可以访问这些元素之前,你需要取消引用它:
if (*((*automaton)+e1)==NULL) ...或者用一种更好的方式来写:
if ((*automaton)[e1] == NULL) {
(*automaton)[e1] = t;
}automaton是指向数组的指针,(*automaton)是数组。
但是,代码的正确性取决于调用函数的方式,您已经将其从示例中删除。下一次写一个完整的例子。
https://stackoverflow.com/questions/47122959
复制相似问题