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 *.

  • thank you for help.I think this is precise & exact answer to my question, but just for sake of clear understanding, does this mean that whatever object we'll create of type GObject will point to void data i.e., nothing. what does this mean & where it is useful. – user299731 Jan 20 '15 at 16:34
  • @user299731 updated my answer. Please take a look at the Udpate section! – Kareem Jan 20 '15 at 17:09
  • thanx a lot again! now i understood the terms correctly & your explanation is just perfect. – user299731 Jan 20 '15 at 17:44
  • sorry for again taking your attention but what does this instruction mean: typedef void proc(*proc)(); – user299731 Jan 20 '15 at 17:55
  • @user299731 do you mean typedef void (*proc)();? – Kareem Jan 20 '15 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .