Michal Michal - 2 months ago 8
C Question

Is it considered as global variable? struct and linked list

I searched, but couldn't find strict answer for my question.
I have to write project using dynamic structures of data, but I can't use global variables . I want to use linked list of linked list. I found this code on the forum (a linked list of linked lists in C) and it works great for me, but I'm not sure if this part is considered as global variable.

typedef struct sToy {
char name[50];
struct sToy *next;
}tToy;

typedef struct sChild {
char name[50];
tToy *firstToy;
struct sChild *next;
} tChild;


I mean the "tToy" and "tChild" thing. Couse head has been declared in the main.

Thank you for any help.

Answer Source

Like @WhozCraig said, those are struct type aliases, not global variables. Here's a sample code to show how you could use those structs as global variables or local variables:

typedef struct sToy {
    char name[50];
    struct sToy *next;
}tToy;

typedef struct sChild {
    char name[50];
    tToy *firstToy;
    struct sChild *next;
} tChild;

tToy Gtoy;//global
tChild Gchild;//global

int main() {
 tToy Ltoy;//local
 tChild Lchild;//local
}

You could also have omitted the "sToy" and "sChild", leaving just "typedef struct {...} alias;"