There are 3 versions of a header file.
version 1:
typedef struct node
{
void* dataPtr;
struct node* link;
} NODE;
version 2: without old type name 'node' (typedef oldTypeName newTypeName)
typedef struct
{
void* dataPtr;
struct* link;
} NODE;
version 3: without typedef
struct
{
void* dataPtr;
struct* link;
} NODE;
According to C : typedef struct name {...}; VS typedef struct{...} name;, version 1's defining 'node' is superfluous and I changed it to version 2 and it worked fine.
According to this answer again, when not using 'typedef', one cannot reuse 'NODE'.
However, codes that use this version 3 header file worked just fine. (NODE was used two three times.)
This is the code:
/* Create a list with two linked nodes.*/
#include <stdio.h>
#include <stdlib.h>
#include "version3.h" //Header file
int main (void)
{
//Local Definitions
int* newData;
int* nodeData;
NODE* node;
// Statements
newData = (int*)malloc (sizeof (int));
*newData = 7;
node = createNode(newData);
newData = (int*)malloc (sizeof (int));
*newData = 75;
node->link = createNode(newData);
nodeData = (int*)node->dataPtr;
printf ("Data from node 1: %d\n", *nodeData);
nodeData = (int*)node->link->dataPtr;
printf ("Data from node 2: %d\n", *nodeData);
return 0;
} //main
What's the use of 'typedef' in this situation? (assuming that here I reused NODE. -> if this assumption is not true, please tell me why. I'm not familiar with C language.)