The Wonderful Uses of typeof in C Language

The typeof() feature provided by GCC C allows you to obtain the type of a variable or the type of an expression. You can refer to:https://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/C-Extensions.html#C-Extensions This article summarizes the common usages of the typeof() keyword and provides corresponding examples to deepen understanding.There are several common usages of the typeof() keyword:1. You can use typeof() to define a variable that receives the return value of a function without knowing what type the function returns.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct apple{    int weight;    int color;};
struct apple *get_apple_info(){    struct apple *a1;    a1 = malloc(sizeof(struct apple));    if(a1 == NULL)    {        printf("malloc error.\n");        return;    }
    a1->weight = 2;    a1->color = 1;
    return a1;}
int main(int argc, char *argv[]){    // Define a variable r1 to receive the value returned by the function get_apple_info(). Since the return type of this function is: struct apple *, the variable r1 is also of that type. Note that the function will not execute.
    typeof(get_apple_info()) r1;
    r1 = get_apple_info();
    printf("apple weight:%d\n", r1->weight);    printf("apple color:%d\n", r1->color);
    return 0;}

2. Dynamically obtain the type of related structure members in macro definitions. In the following code, a temporary variable _max1 is defined with the same type as variable x, and a temporary variable _max2 is defined with the same type as variable y. Then, it checks whether the types of the two are consistent; if not, a warning is given, and finally, the two are compared.

#define max(x, y) ({            
    typeof(x) _max1 = (x);      
    typeof(y) _max2 = (y);      
    (void) (&_max1 == &_max2);  
    // If the caller passes parameters of different types, a warning will be issued at compile time.
    _max1 > _max2 ? _max1 : _max2; })

In the following code, the passed a and b are not of the same type.

int main(int argc, char *argv[]){    int a = 3;    float b = 4.0;    int r = max(a, b);
    printf("r:%d\n", r);
    return 0;}

At this point, a warning will appear during compilation:

[root@xx c_base]# gcc test.c
test.c: In function 'main':
test.c:43: warning: comparison between distinct pointer types lacks a cast

3. You can also directly obtain known types. In the following code, an int type pointer p is defined; this usage is not very meaningful.

    int a = 2;    typeof (int *) p;    p = &a;    printf("%d\n", *p);

4. Other usages

    // Other usage 1    char *p1;    typeof (*p1) ch = 'a';  // ch is of type char, not char * type.
    printf("%d, %c\n", sizeof(ch), ch); // 1, a
    // Other usage 2    char *p2;    typeof (p2) p = "hello world"; // At this point, p is of type char *; since on a 64-bit machine, the pointer size is 8 bytes.
    printf("%d, %s\n", sizeof(p), p); // 8, hello world

5. Conclusion The above examples do not exhaust all situations, but the core usages are basically covered. Other examples can also be referenced online.

Original article: https://blog.csdn.net/rosetta/article/details/90741468

Leave a Comment