这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » C语言typeof的妙用

共9条 1/1 1 跳转至

C语言typeof的妙用

高工
2023-03-05 09:47:22     打赏
    typeof() 是GUN C提供的一种特性,它可以取得变量的类型,或者表达式的类型。可以参考:
https://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/C-Extensions.html#C-Extensions

    本文总结了typeof()关键字的常见用法,并给出了相应的例子,以加深理解。
typeof()关键字常见用法一共有以下几种1. 不用知道函数返回什么类型,可以使用typeof()定义一个用于接收该函数返回值的变量。


#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[]){    //定义一个变量r1,用于接收函数get_apple_info()返回的值,由于该函数返回的类型是:struct apple *,所以变量r1也是该类型。注意,函数不会执行。    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. 在宏定义中动态获取相关结构体成员的类型    如下代码,定义一个和变量x相同类型的临时变量_max1,定义一个和变量y相同类型的临时变量_max2,再判断两者类型是否一致,不一致给出一个警告,最后比较两者。

#define max(x, y) ({            \    typeof(x) _max1 = (x);      \    typeof(y) _max2 = (y);      \    (void) (&_max1 == &_max2);  \//如果调用者传参时,两者类型不一致,在编译时就会发出警告。    _max1 > _max2 ? _max1 : _max2; })

    如下代码,传入的a和b不是同一类型。


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;}

    此时编译就会出现警告:

[root@xx c_base]# gcc test.ctest.c: 在函数‘main’中:test.c:43: 警告:比较不相关的指针时缺少类型转换

3. 也可直接取得已知类型    如下代码,定义了一个int类型的指针p,像这种用法没什么太大的意义了。

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

4. 其它用法


    //其它用法1    char *p1;    typeof (*p1) ch = 'a';  // ch为char类型,不是char *类型。    printf("%d, %c\n", sizeof(ch), ch); // 1, a
    //其它用法2    char *p2;    typeof (p2) p = "hello world"; // 此时的p才是char *类型,由于在64位机器上,所以指针大小为8字节    printf("%d, %s\n", sizeof(p), p); // 8, hello world

5. 总结
    以上例子并没有穷举所有的情况,但其核心用法基本上就会了,其它的例子也可参考网上的例子。





关键词: C语言     typeof     妙用    

专家
2023-03-05 09:58:50     打赏
2楼

感谢分享


专家
2023-03-05 11:13:55     打赏
3楼

谢谢分享


高工
2023-03-05 12:29:46     打赏
4楼

感谢分享


专家
2023-03-05 13:05:52     打赏
5楼

感谢分享


高工
2023-03-05 13:14:03     打赏
6楼

感谢分享


高工
2023-03-05 13:17:54     打赏
7楼

感谢分享


院士
2023-03-06 08:09:19     打赏
8楼

感谢分享


专家
2023-03-06 08:15:56     打赏
9楼

谢谢分享


共9条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]