0

when i try to run this program i get a error message : 13 unknown type name 'node' i using codeblocks 13.12 in lubuntu 16.04.
note : i see no added settings in the compiler "all is uncheck" also im looking for a good IDE for lubuntu 16.04 .

 #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;
}
2501
  • 25,460
  • 4
  • 47
  • 87
jaindoe
  • 69
  • 2
  • 10
  • 1
    If you want to make comments, leave a comment on either your question or one of the answers you want to respond to. Don't post an answer which is really a comment, and don't edit the answers of others to add comments. – dbush Jun 02 '16 at 12:27
  • 1
    Don't post a change that completely alters the question. Doing so invalidates the existing answers. I've rolled back your most recent edit. If your original question was answered, accept one of the answers and post a new question. – dbush Jun 03 '16 at 01:06
  • I have already reverted your edits twice. I have written a comment on the other question. Please read it, and the above two comments on why you shouldn't do this. Let me repeat what dbush said: *If your original question was answered,* (and it was) *accept one of the answers and post a new question.* – 2501 Jun 03 '16 at 08:13

2 Answers2

3

You need to typedef it to create an alias, else you must use struct node since that's the type's name.

typedef struct node node;

And please don't cast the return value of malloc() in C. The allocation is better written like this:

node* temp = malloc(sizeof *temp);
Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
3

The error is at line 13, as the compiler stated:

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

Structure types must always be prefixed by the struct keyword as follows:

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

That however is just the first of several problems in the code:

  • The preceding line has (head = NULL) for an if condition. This is an assignment, not a comparison. It should instead be (head == NULL).
  • No closing brace at the end of function insert.
  • In main, i is undefined. Also, c is not used. So change c to i.
  • In main you're calling Insert instead of insert.
  • No closing brace for the for block in main
  • You need to #include <stdlib.h> to get the prototype for malloc.

Fix those, and your program will compile

dbush
  • 205,898
  • 23
  • 218
  • 273
  • this is another problem i encountered... duplicated first output i juts cant understand the logic why its happen i appreciate any explanation – jaindoe Jun 03 '16 at 00:23