I was assigned to work on some code, but I was given part of it beforehand. It includes this part of code:
typedef int ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node {
ElementType Element;
Position Next;
Position Previous;
};
The way I understand it is,
typedef
PtrToNode as a pointer of the structure called Node.typedef
List as PtrToNote. Which should mean that List is also a pointer to the structure called Node. This pointer is called List. (Confusion #2)Can someone please explain to me points #1, 2, 4, and 6? Am I understanding the code correctly?
Instead of writing int we now can write ElementType to define a value as an integer. (Confusion #1)
This is correct - for example:
int a = 50;
Is now equivalent to
ElementType a = 50;
We have defined a structure called Node. (I am unsure why it wasn't defined here).
This is (forward) declaring a structure called Node, not defining a structure
We typedef PtrToNode as a pointer of the structure called Node.
Your understanding here is also correct. Thus the following are equivalent:
struct Node *x;
PtrToNode x;
We typedef List as PtrToNote. Which should mean that List is also a pointer to the structure called Node. This pointer is called List. (Confusion #2)
List
and PtrToNode
represent the same type - the following examples all do the same thing:
struct Node *x;
PtrToNode x;
List x;
Similar to number 4, but this one is called Position.
Correct.
We define how the structure is going to be set up. In this case Element is supposed to be an int, as per our previous definition. Next and Previous are supposed to be pointers of Position? However, if I were to change Position with List, how would this affect the code?
This is defining the structure Node
. Replacing Position
with List
would have no effect on the code. To two types are compatible.