Ошибка разыменования при печати элемента, на который указывает голова в связанном списке - PullRequest
0 голосов
/ 23 сентября 2018

Я пытаюсь напечатать элемент узла, на который указывает head, но в результате я получаю ошибку «разыменование указателя на неполный тип» struct nodeStruct.Код, который не показан, включает файл list.h и другие функции класса.

Соответствующий код: List.c:

struct nodeStruct {
    int item;
    struct nodeStruct *next;
};
/*
 * Allocate memory for a node of type struct nodeStruct and initialize
 * it with the value item. Return a pointer to the new node.
 */
struct nodeStruct* List_createNode(int item) {
    struct nodeStruct *newNode = malloc(sizeof(struct nodeStruct));
    newNode->item = item;
    newNode->next = NULL;
    printf("New node created with item %d\n", newNode->item);
    return newNode;
}


/*
 * Insert node at the head of the list.
 */
void List_insertHead (struct nodeStruct **headRef, struct nodeStruct *node) {
    if(*headRef == NULL) {
        printf("List is empty, creating new head\n");
        *headRef = node;
        printf("Empty list new head: %d\n", (*headRef)->item);
    }
    // Head already exists, shift pointer of head to new node
    // and change new head pointer to old head
    else {
        struct nodeStruct* oldHead = *headRef;
        printf("Old head item: %d\n", oldHead->item);
        node->next = *headRef;
        *headRef = node;
        printf("New Head: %d // Old head: %d\n", (*headRef)->item, node->next->item);
    }
}

test_list.c:

int main(int argc, char** argv)
{
printf("Starting tests...\n");
struct nodeStruct* head = NULL;

// Create 1 node:
struct nodeStruct* firstNode = List_createNode(0);
List_insertHead(&head, firstNode);
printf("%d\n", head->item); // error

Makefile:

CC=cc
CXX=CC
CCFLAGS= -g -w -std=c99 -Wall -Werror



all: test_list test

# Compile all .c files into .o files
# % matches all (like * in a command)
# $< is the source file (.c file)
%.o : %.c
    $(CC) -c $(CCFLAGS) $<


test_list: list.o test_list.o
    $(CC) -o test_list list.o test_list.o

test: test_list
    ./test_list


clean:
    rm -f core *.o test_list

Назначение печатающей головки-> элемент - проверить, правильно ли работает головка.

Ответы [ 2 ]

0 голосов
/ 23 сентября 2018

Вам не хватает файла заголовка.

C компилирует каждый исходный файл в объектные файлы отдельно.Тогда это связывает их вместе.Каждый файл .c должен знать сигнатуру всех типов и функций, которые он использует.Это означает, что test_list.c должен знать сигнатуры структур и функций в list.c.В настоящее время это не так.

Вы можете напрямую включить list.c в test_list.c с #include list.c, который в основном вставляет list.c в test_list.c.Это будет работать, но тогда list.c не может использоваться никаким другим файлом, не вызывая всевозможных проблем.

Лучше создать файл заголовка , который объявляет все ваши типы и forward объявляет все ваши функции.Предварительная декларация позволяет компилятору узнать, какие функции доступны и какова их подпись, с обещанием, что функция будет определена позже чем-то другим.

// list.h

struct nodeStruct {
    int item;
    struct nodeStruct *next;
};

struct nodeStruct* List_createNode(int);
void List_insertHead (struct nodeStruct **, struct nodeStruct *);

Теперь и test_list.c, и list.c могут #include "list.h" предоставить компилятору достаточно информации для компиляции каждого исходного файла в объектный файл.Затем объекты будут связаны между собой и сообщат test_list.o, где найти функции в list.o.

# Compile list.c into list.o using the declarations in list.h
cc -c -g -w -std=c99 -Wall -Werror list.c

# Compile test_list.c into test_list.o using the declarations in list.h
cc -c -g -w -std=c99 -Wall -Werror test_list.c

# Link both object files into an executable so test_list.o can
# use the functions compiled into list.o
cc -o test_list list.o test_list.o
0 голосов
/ 23 сентября 2018

Ваш код работает нормально:

посмотрите результаты:

Starting tests...
New node created with item 0
List is empty, creating new head
Empty list new head: 0
0

Возможно, это что-то с вашим процессом компиляции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...