Technical Note : SLE0007
Author : Scott Evans
Created/Modified : 4/9/98
Description : Useful C extensions 

This technical note describes extensions to the C language supported by GNU compilers.

Pointer arithmetic

With GNU C you can perform arithmetic on void pointers and functions. The sizeof() can also be used on void pointers. It assumes void pointers are the same as byte pointers. So sizeof() will return 1.


void *vp;

vp=(void *)0x80090000;

printf(“Size of void pointer = %d\n”,sizeof(void *));
printf(“Address = %x\n”,vp);
printf(“Address = %x\n”,vp);

Case ranges

In case statements you can specify a range of values with ‘...’.


 case ‘A’ ... ‘Z’:
  puts(“Upper case”);

 case ‘a’ ... ‘z’:
  puts(“Lower case”);

 case ‘0’ ... ‘9’:

Array element ranges

It is possible to assign a value to a range of array elements. The ‘...’ is used.


int a[30]={[0 ... 9]=23,[10 ... 12]=0,[20 ... 29]=100};

This will fill the first 10 elements of the array with 23. Elements 10-12 will be set to 0 and 20-29 will be 100.

You can also do the following.

int a[10]={[6]=23,[8]=250,[0]=21};

This is the same as the following bit of code.

int a[10]={21,0,0,0,0,0,23,0,250,0};

Alignment of variables

The __alignof__ keyword can be used to find out how a type should be aligned. For example the following will return 4 since a long on R3000 needs to be 4 byte aligned.

printf(“Alignemnt of long %d\n”,__alignof__(long));

You can set the alignment of a variable using the __attribute__ keyword.


long a __attribute__ ((aligned(4)))=21;

This will make sure the variable a is aligned on a 4 byte boundary. It also sets the value of a to 21.

Address of labels

The address of a label can be found using the ‘&&’ operator. It is returns type void *.


void main(void)
 static void *jump_table[]={&&here,&&there,&&everywhere};

 goto *jump_table[0];
 puts(“Never getes here...”);
 goto *jump_table[1];
 puts(“or here...”);
 goto *jump_table[2];
 puts(“or here...”);

 This document in word format.
This document in text format.