Question

I want to know if its possible to use gcc extensions in codeblocks like typeof in the windows environment.

The code below is meant just to show an example of how I want to use typeof.

#include <stdio.h>
#include <stdlib.h>
#define SWAP(x, y) do { typeof(x) temp##x##y = x; x = y; y = temp##x##y; } while (0)

typedef struct Away{
    int var1;
    char cc;
    int array[10];
} somedatatype;

void print_data(somedatatype data){
    printf("var1 = %d\ncc = %c\narray[10] = {",data.var1,data.cc);
    for(int i=0;i<10;i++){
       i!=9 ? printf("%d,",data.array[i]):printf("%d}",data.array[i]);
    }
}

int main(){
    somedatatype data1, data2;
    int a=51,b=42;
    //initialize data1
    data1.var1=2;
    data1.cc='k';
    for(int i=0;i<10;data1.array[i]=5,i++);
    //initialize data2
    data2.var1=3;
    data2.cc='y';
    for(int i=0;i<10;data2.array[i]=4,i++);
    //swap
    SWAP( data1, data2);
    SWAP( a, b);
    //print everything
    printf("data1:\n");
    print_data( data1);
    printf("\ndata2:\n");
    print_data( data2);
    printf("\na = %d\nb = %d\n");
}

PS: I couldn't find a way while reading through http://gcc.gnu.org/onlinedocs/gcc/Typeof.html#Typeof

Was it helpful?

Solution

Literally - yes, it is possible.

This gcc extension available in GNU C. Specify it with -std=gnu99 (because you also using int within for declaration, C89 is not an option here).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top