list.h
#ifndef LIST_H
#define LIST_H
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
list.c
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct {
int item;
struct nodeStruct *next;
};
struct nodeStruct* List_createNode(int item) {
struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
if (node == NULL) {return NULL;}
node->item = item;
node->next = NULL;
return node;
}
Main.c :
#include "list.h"
#include <assert.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct nodeStruct *one = List_createNode(1);
while(one != NULL) {
printf("%d", one->item); //error
one= one->next; //error
}
오류 : error: dereferencing pointer to incomplete type printf("%d", one->item);
오류는에 one->item
있습니다. 여러 조합을 역 참조하려고 시도했지만 작동하지 않는 것 같습니다. 올바른 접근 방식은 무엇입니까?
업데이트 :
list.h
#ifndef LIST_H
#define LIST_H
struct nodeStruct {
int item;
struct nodeStruct *next;
};
/* Function prototypes */
struct nodeStruct* List_createNode(int item);
#endif
이제 오류는 invalid application of ‘sizeof’ to incomplete type ‘struct nodeStruct’ struct nodeStruct *node = malloc(sizeof(struct nodeStruct));
내 list.c 파일에서입니다.
몇 가지 방법을 생각할 수 있습니다.
의 정의를 넣어 struct
헤더 파일에 #include
있는 헤더 파일 main.c
.
몇 가지 기능 추가
int getNodeItem(struct nodeStruct* node)
{
return node->item;
}
struct nodeStruct* getNextNode(struct nodeStruct* node)
{
return node->next;
}
에서 함수를 호출합니다 main
.
while (one != NULL) {
printf("%d", getNodeItem(one));
one = getNextNode(one);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다