GNU C Compiler Internals/GCC Hacks 3 4

toString() method for each structure as in JavaEdit

Invoke a block of code from a function as in RubyEdit

Linux implementation of lists allows to invoke a block of code on each element of the list:


       list_for_each_prev(pos, head) {
                struct nfs_page *p = nfs_list_entry(pos);
                if (page_index(p->wb_page) < pg_idx)

list_for_each_prev takes the code in the brackets as a parameter. The trick is to use a macro that rolls out to a for() loop whose body becomes the code in the brackets. The goal of this project is to allow programmers to use code blocks in function calls.

Dereference function results when a structure is returnedEdit

C allows one to dereference the return value of a function if it is a pointer to a structure:


If the function returns the structure, not a pointer to it, then a compile-time error is generated:

 > request for member `field' in something not a structure or union

This extension addresses the problem of dereferencing structures that are return values.

Use functions to initialize a variableEdit

When a variable is defined and initialized the initializer is constant. You will get an error if you try to use a function, no matter what this function is:

 int getint() { return 1; }
 int i=getint();
 > initializer element is not constant

When a variable is used the function it was initialized with is called.

Default values of function arguments as in C++Edit

 void func(int a=0) {
   printf("a=%d\n", a);
 int main() {
 > syntax error before '=' token

Reference parameters as in C++Edit

 void test(int &a, int &b);
 int x,y;

GCC switches in object fileEdit

GCC switches in object file

Type information at run-timeEdit

There is no type information in C language available at run-time. The idea is to allow program to get the names and offsets of a structure's field at run time, the symbolic name of an enum declaration, etc. For example, instead of writing

  enum tree_code code;
  switch (code) {
    case VAR_DECL:
    case BLOCK:

one could write

  printf("%s\n", type_info(code).name);