I'm little confused about the syntax of typedefs. Can anybody tell me what these lines of code mean
typedef struct GWindowCDT *GWindow;
typedef void *GObject;
The typedef
keyword defines a type synonym for an existing type — it basically defines a new name for an existing type. The original type name and its type synonym can be interchangeably used in your program. The statement
typedef struct GWindowCDT *GWindow;
basically defines the type synonym GWindow
for the existing pointer type GWindowCDT *
. Similarly the statement
typedef void *GObject;
defines the type synonym GObject
for the existing pointer type void *
.
The purpose of this is basically to hide pointers so that you don't have to deal with them. Many higher-level languages follow the same approach (e.g., Java).
Update: void *
is a pointer type. The keyword void
here refers to the lack of type not the lack of data as you expected.
You can think of this as an abstract, general pointer type. If you read the man page for a function like malloc
for example, you will find that it returns a value of the same type (i.e., void *
). Yet we can still assign the value that it returns to variables of various pointer types (e.g., int *
, char *
, etc.).
So a value of type void *
may be casted to a more specific pointer type.
The SPL is originally written in Java. GObject
is what Java programmers call a super type (aka a parent type). Many other types inherit from (aka extend) the type GObject
(e.g., GWindow
, GRect
, etc.). So GObject
represents the general type and that is why at the C level it is a type synonym for void *.