Search code examples
cpointersstructheader-files

Pointer to custom struct typedef issue


As a simple beginner project, I am trying to build a linked list. I have created a structure typedef for the node. My issue is that I want another typedef as a pointer to a list node, for some reason, this is not working and I am very confused. I have read other answers on here and none of them have worked.

main.c

#include <stdio.h>
#include "linkedList.h"


int main(int argc, char **argv){
    return 0;
}

linkedList.h

#ifndef linkedList
#define linkedList

typedef struct listStrNode {
    char *string;
    struct listStrNode *next;
} listStringNode;

typedef listStringNode *linkedList;

#endif

error

In file included from main.c:3:
linkedList.h:9:35: error: expected identifier or ‘(’ before ‘;’ token
    9 | typedef listStringNode *linkedList;
      |

compiled with:

gcc main.c

any ideas?


Solution

  • #define linkedList
    

    That will replace all instances of linkedList with an empty string. Which effectively means typedef listStringNode *linkedList; becomes typedef listStringNode *; and hence the error. To fix, rename either the #define or the type name. A common convention is to use a representation of the filename as the include guard: e.g. #define LINKEDLIST_H