我希望这会提前帮助你。首先,对不起我的英语。它可能有几个语法或拼写错误。
我在你的代码中看到的问题主要是你混合了一个指针的定义和同一个指针的实现。
从 ANSI C 到 C99,即使在 C++ 中(未在 C# 中测试),也有一个使用指针的大技巧可能会提前有所帮助:认为指针是向量的第一个元素,即[0] one。
一个解释这个概念的好网站是:http ://boredzo.org/pointers/
这个 hack 是一个简单的翻译,是一个很好的 hack 工具,可以更好地理解指针。
开始行动吧,孩子们。
用于将元素添加到列表中的函数,
int add_action_to_queue(Action newAction, int newValue, QueuedAction **lastAction)
{
QueuedAction *newQueuedAction;
// Create a new action in memory
if ((newQueuedAction = (QueuedAction *)malloc(sizeof(QueuedAction))) == NULL)
return 0;
// Make the old 'lastAction' point to the new Action,
// and the new Action to point to NULL:
*lastAction -> nextAction = newQueuedAction;
newQueuedAction -> nextAction = NULL;
newQueuedAction -> action = newAction;
newQueuedAction -> value = newValue;
// Designate the new Action as the new lastAction:
*lastAction = newQueuedAction;
return 1;
}
包含一些错误。首先,考虑使用
*lastAction -> ...;
作为
lastAction[0] -> ...;
如您所见,您不能使用->运算符来访问内部元素。
同样的情况也发生在这里:
lastAction[0] = newQueuedAction;
您不能在结构内复制指针。lastAction,使用这个技巧来更好地理解,不再是一个指针 - 事实上,它是编译器分配给那里的结构中第一个元素的内容 - 所以你也需要更改这一行,以更改指针值:
lastAction = newQueuedAction;
使用此翻译和注释,您的代码现在将是:
int add_action_to_queue(Action newAction, int newValue, QueuedAction **lastAction)
{
QueuedAction *newQueuedAction;
// Create a new action in memory
if ((newQueuedAction = (QueuedAction *)malloc(sizeof(QueuedAction))) == NULL)
return 0;
// Make the old 'lastAction' point to the new Action,
// and the new Action to point to NULL:
lastAction[0] -> nextAction = newQueuedAction;
newQueuedAction -> nextAction = NULL;
newQueuedAction -> action = newAction;
newQueuedAction -> value = newValue;
// Designate the new Action as the new lastAction:
lastAction[0] = newQueuedAction;
return 1;
}
现在,错误是可见的:您尝试错误地使用->运算符。这意味着您的代码将以两种方式更改:
它看起来像这样:
int add_action_to_queue(Action newAction, int newValue, QueuedAction **lastAction)
{
QueuedAction *newQueuedAction;
// Create a new action in memory
if ((newQueuedAction = (QueuedAction *)malloc(sizeof(QueuedAction))) == NULL)
return 0;
// Make the old 'lastAction' point to the new Action,
// and the new Action to point to NULL:
lastAction -> nextAction = newQueuedAction;
newQueuedAction -> nextAction = NULL;
newQueuedAction -> action = newAction;
newQueuedAction -> value = newValue;
// Designate the new Action as the new lastAction:
lastAction = newQueuedAction;
return 1;
}
它看起来像这样:
int add_action_to_queue(Action newAction, int newValue, QueuedAction **lastAction)
{
QueuedAction *newQueuedAction;
// Create a new action in memory
if ((newQueuedAction = (QueuedAction *)malloc(sizeof(QueuedAction))) == NULL)
return 0;
// Make the old 'lastAction' point to the new Action,
// and the new Action to point to NULL:
lastAction[0].nextAction = newQueuedAction;
newQueuedAction[0].nextAction = NULL;
newQueuedAction[0].action = newAction;
newQueuedAction[0].value = newValue;
// Designate the new Action as the new lastAction:
lastAction = newQueuedAction;
return 1;
}
并且不要忘记擦除== NULL代码,您会遇到一个将 NULL 定义为其他东西的被动攻击型程序员。始终使用大括号以确保可扩展性。这一行只是代码风格的推荐。
希望能帮助到你,