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

main()
{  
    struct node
    {   
         int data;
         struct node *next;
    };

    struct node *first=(struct node*)malloc(sizeof(struct node));
    struct node *second=(struct node*)malloc(sizeof(struct node));
    struct node *third=(struct node*)malloc(sizeof(struct node));

    scanf("%d %d %d",&(first->data),&(second->data),&(third->data));
    first->next=second;
    second->next=third;
    third->next=NULL;
    struct node *t=(struct node *)first;
    f(t);
 }

f(struct node *a)
{
    while(a!=NULL)
    {
        printf("%d",a->data);
        a= a->next;
    }
}

the code above is giving a warning and an error "struct node declared inside parameter list" and "dereferencing pointer to incomplete type"

kindly help me making the code run and solving error.

mes
  • 3,581
  • 29
  • 28

2 Answers2

2

There are several issues.

  • your functions don't have a return type
  • your struct node is declared only in the scope of main.
  • you use the f function before it has been declared.
  • last but not least: the formatting of your code is horrible. Formatting isn't important for the compiler, but only for the human reader including you

This is what your program should look like:

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

struct node               // structure declared at global scope
{
  int data;
  struct node *next;
};

void f(struct node *a);   // declare function

int main()                // function has now return type int
{
  struct node *first = (struct node*)malloc(sizeof(struct node));
  struct node *second = (struct node*)malloc(sizeof(struct node));
  struct node *third = (struct node*)malloc(sizeof(struct node));

  scanf("%d %d %d", &(first->data), &(second->data), &(third->data));
  first->next = second;
  second->next = third;
  third->next = NULL;
  struct node *t = (struct node *)first;
  f(t);
}

void f(struct node *a)   // function has now return type void
{
  while (a != NULL)
  {
    printf("%d", a->data);
    a = a->next;
  }
}

Disclaimer: this program just compiles correctly without warnings, but I didn't check if it actually makes sense.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • The definition of the `f` function actually **is** outside the `main` function. And _that_ is why `f` doesn't see the `struct node` declaration. – CiaPan Jun 29 '18 at 08:41
  • @CiaPan oh, you're right. Edited. That's why the last point of my list is important ;-) – Jabberwocky Jun 29 '18 at 08:42
  • I agree. I was going to answer this question too, but you were faster. And my answer was essentialy same as yours, except I put the last point at the top of the list. Programs are for computers, but the source code is for humans. So it should always be as readable as possible. – CiaPan Jun 29 '18 at 08:46
  • @Jabberwocky it compiled and made a sense too ;-) it is a program to print a linkedlist recursively. – Shivam Singh Jun 29 '18 at 08:47
  • @ShivamSingh yes, it seems to be correct, but there is no recursion though. – Jabberwocky Jun 29 '18 at 08:48
  • i have a question why do we need to declare function and struct outside main body... – Shivam Singh Jun 29 '18 at 08:49
  • what if we write it as void f(struct node *a) {if (a){ printf("%d",a->data); f(a->next) will it be recursive – Shivam Singh Jun 29 '18 at 08:50
  • @ShivamSingh 1. nested functions are not standard C, 2. if the struct is declaire inside the `main` function, it is only visible from inside the `main` function, and that is clearly not what you need here. – Jabberwocky Jun 29 '18 at 08:50
  • @ShivamSingh please ask another question. This is a different matter. Don't put code in comments, it's hardly readable. – Jabberwocky Jun 29 '18 at 08:54
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ===== Define structure outside function ===== */
/*
    Use `typedef` to rename the data type.
    `node_t` is equal with `struct node_t`, and
    `node` is equal with `struct node_t *`.
 */
typedef struct node_t
{
    int data;
    struct node_t *next;
} node_t, *node;

/*  Define link list
 */
typedef struct list_t
{
    node head;
    node rear;
    int size;
} list_t, *list;

list list_create()
{
    list l = NULL;

    if ((l = malloc(sizeof(list_t))) == NULL)
    {
        exit(-1);
    }
    memset(l, 0, sizeof(list_t));

    return l;
}

int list_destroy(list *pl)
{
    node cur;
    node prev;

    if (pl == NULL || *pl == NULL)
        return -1;

    cur = (*pl)->head;
    while (cur != NULL)
    {
        prev = cur;
        cur = cur->next;
        free(prev);
    }

    free(*pl);
    *pl = NULL;

    return 0;
}

int list_push(list l, int data)
{
    node n;

    if (l == NULL)
        return -1;

    if ((n = malloc(sizeof(node_t))) == NULL)
    {
        exit(-1);
    }

    n->data = data;
    n->next = NULL;

    if (l->head == NULL)
    {
        l->head = n;
    }
    else
    {
        l->rear->next = n;
    }

    l->rear = n;
    l->size++;

    return 0;
}

int list_pop(list l, int *pdata)
{
    if (l == NULL || l->head == NULL)
    {
        return -1;
    }

    *pdata = l->head->data;
    if (l->head == l->rear)
    {
        l->rear = NULL;
    }
    l->head = l->head->next;

    l->size--;

    return 0;
}

void list_foreach(list l, void (*func)(int data, void *arg), void *arg)
{
    node cur = l->head;

    while (cur != NULL)
    {
        func(cur->data, arg);
        cur = cur->next;
    }
}


static void print_node(int data, void *arg)
{
    printf("%d\n", data);
}

int main(int argc, char *argv[])
{
    int i;
    int d;
    list l;

    l = list_create();

    for (i = 0; i < 5; i++)
    {
        scanf("%d", &d);
        getchar();
        list_push(l, d);
    }

    list_foreach(l, print_node, NULL);

    printf("Pop: [status %d], ", list_pop(l, &d));
    printf("[value %d]\n", d);

    printf("Pop: [status %d], ", list_pop(l, &d));
    printf("[value %d]\n", d);

    list_foreach(l, print_node, NULL);

    list_destroy(&l);

    return 0;

}
Joy Allen
  • 402
  • 3
  • 8