使用指针结构时出错

时间:2013-10-29 19:07:42

标签: c

我的代码在这里给出了编译时错误 - "取消引用指向不完整类型的指针"在函数InsertList中。我无法弄清楚原因。我做错了什么?

#include<stdio.h>
#include<stdlib.h>

struct ListNode;
struct ListNode{
int data;
struct ListNode* next;
};

void main(int argc,char* argv[]){

int p;
FILE *ptr;
FILE *out;

//ptr=fopen("C:\Users\dheeraj\Desktop\input.txt","r");
out=fopen("output.txt","w");

struct Listnode* head=0;

while(fscanf(ptr,"%d",&p) != EOF){
InsertList(head,p);
}
close(ptr);
}

void InsertList(struct Listnode** headref,int data)
{

struct Listnode* newNode= malloc(sizeof(struct ListNode));
if(newNode == 0)
    printf("Memory error\n");

newNode->data=data;
newNode->next = (*headref);
(*headref )=newNode;

}

1 个答案:

答案 0 :(得分:3)

void InsertList(struct Listnode** headref,int data)

应该是:

void InsertList(struct ListNode** headref,int data)

此外:

struct Listnode* newNode= malloc(sizeof(struct ListNode));

应该是:

struct ListNode* newNode= malloc(sizeof(struct ListNode));

Listnode进行全局搜索,并替换为ListNode