c - 解引用指向不完整类型的指针未编译

标签 c

所以我正在编写一个程序,它使用双链表和节点来遍历它们,同时执行简单的数学运算和添加/删除类型的内容。

我们一遍又一遍地检查代码和算法,试图找到逻辑问题,但我们什么也看不到。当我们进行构建时,它显示了大约 43 个错误,其中大部分是关于我用于遍历列表的临时节点的“取消引用”问题。我们有一个 .c 文件和一个 .h 文件。

typedef 结构体在 .h 中定义,#include 在 .c 中定义。我们只是无法弄清楚问题出在哪里。我在这个粘贴箱中链接了代码:http://pastebin.com/PLT3K8kX

void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){

    struct DNode* newNode = calloc(list, sizeOf(newElement));
    newNode->data = newElement;

    int counter = 0;

    struct _DNode* current = list->head;

    while(counter < list->length){

                    if(counter == position){

                            current->next = newNode;
                            newNode->prev = current;
                            newNode->next = current->next;
                            current->next->prev = newNode;

                    }
                    counter++;
                    current = current->next;
            }

}

这是一个示例函数,该程序是由该示例函数组成的。当变量“NewNode”指向下一个或上一个时,就会出现取消引用问题。我无法真正弄清楚实际问题是什么,因为所有 typedef 都列在 .h 中。

我的代码中的示例 typedef:

typedef struct _DNode{

Object data;

struct _DNode* prev;

struct _DNode* next;

} DNode;

最佳答案

您遇到的最大问题是在 DoubleLinkedList typedef 之前继续包含 struct ,这会导致问题。 DNode 也会出现同样的问题。它渗透到代码中。您需要阅读typedef struct vs struct definitions就代码而言,您需要重新访问所有 calloc 调用。目前尚不清楚您要做什么(是的,我知道您正在分配 DoubleLinkedListDNode,但您尝试的操作不正确。

既然如此,我就可以编译你的双链表代码了。除了提供差异之外,没有其他简单的方法可以显示更改。 (使用 diff -uNrb 创建)这将帮助您开始:

--- DoubleLinkedList.c
+++ DoubleLinkedList.c  2014-06-26 22:59:35.768919428 -0500
@@ -19,13 +19,13 @@
#include "DoubleLinkedList.h"


-typedef struct DNode mainTemp;
-typedef struct DoubleLinkedList mainList;
+DNode mainTemp;
+DoubleLinkedList mainList;

//1 DONE
-DoubleLinkedList* allocDList(uint elementSize){
+DoubleLinkedList* allocDList (uint elementSize) {

-   struct DoubleLinkedList* list = &mainList;
+        DoubleLinkedList* list = &mainList;

        list->head = NULL;
        list->tail = NULL;
@@ -35,18 +35,16 @@

        return list;

-
-
}
//2 DONE
void releaseDList(DoubleLinkedList* list){

-   struct DNode* node = list->head;
-   struct DNode* next = NULL;
+   DNode* node = list->head;
+   DNode* next = NULL;

        while(node){

-       struct DNode* next = node->next;
+       DNode* next = node->next;
                free(node);
                node = next;

@@ -56,12 +54,12 @@
//3 DONE
void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){

-   struct DNode* newNode = calloc(list, sizeOf(newElement));
+   DNode* newNode = calloc (1, sizeof(newNode));  // allocating newNode or list?
        newNode->data = newElement;

        int counter = 0;

-   struct _DNode* current = list->head;
+   DNode* current = list->head;

        while(counter < list->length){

@@ -81,7 +79,7 @@
//4 DONE
void appendDList(DoubleLinkedList* list, Object newElement){

-   struct DNode* newNode = calloc(list, sizeOf(newElement));
+   DNode* newNode = calloc(1, sizeof(newNode));  // allocating newNode or list?
        newNode->data = newElement;

        newNode = list->tail->next; // setting newNode as current tail's next
@@ -95,7 +93,7 @@



-   struct DNode* newNode = (DNode*)calloc(list, sizeOf(newElement));
+   DNode* newNode = calloc(1, sizeof(newElement));

        newNode->data = newElement;

@@ -109,12 +107,12 @@
//6 DONE
DoubleLinkedList* reverseDList(DoubleLinkedList* list){

-   struct DoubleLinkedList* newList = NULL;
-   newList = (struct DoubleLinkedList*) malloc(sizeOf(DoubleLinkedList));
+   DoubleLinkedList* newList = NULL;
+   newList = malloc(sizeof(DoubleLinkedList));

-   struct DNode* temp = NULL;
+   DNode* temp = NULL;

-   temp = (DNode*)malloc(sizeOf(DNode));
+   temp = malloc (sizeof (DNode));

        temp = list->tail;

@@ -136,9 +134,9 @@
//7 DONE
DoubleLinkedList* halfList(DoubleLinkedList* list){

-   struct DNode* slow = list->head;
-   struct DNode* fast = list->head;
-   struct DoubleLinkedList* newList = malloc(uint);
+   DNode* slow = list->head;
+   DNode* fast = list->head;
+   DoubleLinkedList* newList = malloc (sizeof (uint));

        if(list->head != NULL){

@@ -166,7 +164,7 @@
Object removeDList(DoubleLinkedList* list, int position){

        int counter = 0;
-   struct _DNode* temp = list->head;
+   DNode* temp = list->head;

        while(counter < list->length){

@@ -189,11 +187,11 @@
//9 DONE
void printDList(DoubleLinkedList* list){

-   struct _DNode* temp = list->head;
+   DNode* temp = list->head;

        while(temp->next != NULL){

-       printf("%d", temp->data);
+       printf ("%d", temp->data);
                temp = temp->next;

        }

此外,除非您非常需要包含 DoubleLinkedListDNode(具有 void 数据类型),否则使用简单列表会更好。您所做的事情是有效的,但是这样做会使调试变得更加困难。对于简单的双链表示例,请参阅:Doubly Linked List (with C..) 。这是一个公平的例子。

关于c - 解引用指向不完整类型的指针未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443067/

相关文章:

c - 如何更改c中的线程用户?

c - strtok 分配给数组结构失败

c - 从函数传递结构数组

c - 如何使用 C 在 Windows 中执行批处理 (.bat) 文件

使用预处理器比较定义的原始数据

可以知道拆机地址吗

c++ - 简单的C/C++问题

无法在 Mac OSX 上使用 gtk3 FileChooser?

c - 指针:关于指针的查询

无法理解这种计算数字平方的方式