-2

I am new to C programming.

So this is in my header file

typedef struct Rec *node;

This is in my c file

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

typedef struct Rec *List;
typedef struct Listing{

    node *items;
    int numelems;
    struct List *next;

}Listing;

int sum(List L)
{
    if(L->items == NULL)
    {
        return NULL;
    }
    return (L->head + sum(L->tail));
}

int main(void)
{
    return 1;
}

I am getting this error where it says dereferencing pointer to incomplete type at if(L->items == NULL). I know that it is in my struct but I do not know how to fix it. I tried trial and error however I would like to understand what and where my error is.

PS: this program is just for myself learning.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Saifeki
  • 1
  • 2
  • 3
    What's in `header.h`? Does it define `struct Rec` or `struct List`? If not, that's the trouble — your `List` parameter is a `struct Rec *`, and if you've not defined `struct Rec` anywhere, you have problems. Your `struct Listing` contains a `struct List *` which is a pointer to a different type too. Note that in your code, the `List` type is unrelated to the `struct List` type. – Jonathan Leffler Nov 16 '14 at 06:27
  • 1
    `typedef struct Rec *node;` - **no no no no please no!** Don't hide pointers behind (misleadingly differently named) `typedef`s! – The Paramagnetic Croissant Nov 16 '14 at 06:35

1 Answers1

0

If the only item in your header is typedef struct Rec *node;, then that's the trouble. You've not defined struct Rec or struct List anywhere. You've said they exist, but that's all. That makes them incomplete types, and you can't dereference an incomplete structure type because the compiler doesn't know what members are in an incomplete structure type.

Your List parameter is a struct Rec * in disguise, and if you've not defined struct Rec anywhere, you have problems. Your struct Listing contains a struct List * which is a pointer to a different type too. Note that in your code, the List type is unrelated to the struct List type. And note that node and List are both aliases for the type struct Rec *.

Your code in sum() references a head and tail member of the List structure pointer, so your struct Rec needs to contain those names.

In general, it is best to keep structure tags and typedef names closely related. That is:

typedef struct Node Node;

but not:

typedef struct Angel Elephant;

Also, the general consensus is that it is best not to embed pointers in the typedef. That is, it is OK to write the typedefs above, but not OK to use:

typedef struct Fish *Fish;

For example, see Is it a good idea to typedef pointers?

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278