为什么我不能运行我的程序

时间:2016-06-02 12:06:12

标签: c ubuntu codeblocks

当我尝试运行此程序时,我收到一条错误消息: 13个未知类型名称'node'我在lubuntu 16.04中使用了代码块13.12 注意:我看到编译器中没有添加任何设置“all is uncheck”,我也正在为lubuntu 16.04寻找一个好的IDE。

 #include <stdio.h>

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

struct node* head = NULL;

void insert()
{
if(head = NULL) {
    node* temp = (node*)malloc(sizeof(struct node));
    temp -> data = 2;
    temp -> next = NULL;
    head = temp;
}

void print() {

    struct node* temp = head;
    printf("list is: ");
    while (temp != NULL) {

        printf( "%d",temp->data);
        temp = temp->next;
    }
    printf("\n");
}


int main () {

head = NULL;
printf("How Many Numbers?\n");
int a ,b ,c;
scanf("%d" , &b);
for(a = 0;i<b;a++) {
    printf("Enter the number \n");
    scanf("%d",&b);
    Insert(b);
    print();
return 0;
}

2 个答案:

答案 0 :(得分:3)

您需要typedef来创建别名,否则必须使用struct node,因为这是类型的名称。

typedef struct node node;

please don't cast the return value of malloc() in C。分配更好地写成:

node* temp = malloc(sizeof *temp);

答案 1 :(得分:3)

错误在第13行,正如编译器所述:

node* temp = (node*)malloc(sizeof(struct node));

结构类型必须始终以struct关键字为前缀,如下所示:

// also, don't cast the return value of malloc
struct node* temp = malloc(sizeof(struct node));

然而,这只是代码中的第一个问题:

  • 上一行的(head = NULL)条件为if。这是一项任务,而非比较。它应该是(head == NULL)
  • 函数insert末尾没有右括号。
  • main中,i未定义。此外,未使用c。因此,将c更改为i
  • main Insert您正在拨打insert而不是for
  • main
  • 中的#include <stdlib.h>块没有右括号
  • 您需要malloc才能获得var t = { x: 0, y: 'string', v: 10000 };
  • 的原型

修复这些,你的程序将编译

相关问题