004 C语言高级用法---typeof()关键字

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/rosetta/article/details/90741468
         <!--一个博主专栏付费入口结束-->
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-4a3473df85.css">
                                    <div id="content_views" class="markdown_views prism-github-gist">
                <!-- flowchart 箭头图标 勿删 -->
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                                        <h2><a name="t0"></a><a id="_0"></a>前言</h2>

typeof() 是GUN C提供的一种特性,可参考C-Extensions,它可以取得变量的类型,或者表达式的类型。

本文总结了typeof()关键字的常见用法,并给出了相应的例子,以加深理解 。

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<span class="token operator">-&gt;</span>weight <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
a1<span class="token operator">-&gt;</span>color <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

<span class="token keyword">return</span> a1<span class="token punctuation">;</span>

}

int main(int argc, char *argv[])
{
typeof(get_apple_info()) r1;//定义一个变量r1,用于接收函数get_apple_info()返回的值,由于该函数返回的类型是:struct apple *,所以变量r1也是该类型。注意,函数不会执行。

r1 <span class="token operator">=</span> <span class="token function">get_apple_info</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"apple weight:%d\n"</span><span class="token punctuation">,</span> r1<span class="token operator">-&gt;</span>weight<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"apple color:%d\n"</span><span class="token punctuation">,</span> r1<span class="token operator">-&gt;</span>color<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 在宏定义中动态获取相关结构体成员的类型

    如下代码,定义一个和变量x相同类型的临时变量_max1,定义一个和变量y相同类型的临时变量_max2,再判断两者类型是否一致,不一致给出一个警告,最后比较两者。

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

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

    int main(int argc, char *argv[])
    {
        int a=3;
        float b = 4.0;
        int r = max(a, b);
    
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"r:%d\n"</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    
  • }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    此时编译就会出现警告

    [root@xx c_base]# gcc test.c
    test.c: 在函数‘main’中:
    test.c:43: 警告:比较不相关的指针时缺少类型转换
    
     
     
    • 1
    • 2
    • 3
  • 也可直接取得已知类型

    如下代码,定义了一个int类型的指针p,像这种用法主没什么太大的意义了。

        int a = 2;
        typeof (int *) p;
        p = &a;
        printf("%d\n", *p);
    
      
      
    • 1
    • 2
    • 3
    • 4
  • 其它用法

  • 	//其它用法1
    	char *p1;
        typeof (*p1) ch = 'a'; //ch为char类型,不是char *类型。
        printf("%d, %c\n", sizeof(ch), ch);//1, a
    
    <span class="token comment">//其它用法2</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>p2<span class="token punctuation">;</span>
    <span class="token keyword">typeof</span> <span class="token punctuation">(</span>p2<span class="token punctuation">)</span> p <span class="token operator">=</span> <span class="token string">"hello world"</span><span class="token punctuation">;</span><span class="token comment">//此时的p才是char *类型,由于在64位机器上,所以指针大小为8字节</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d, %s\n"</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">,</span> p<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//8, hello world</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    总结

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

    参考文章

    C语言中typeof()函数的用法

    C-Extensions

                                    </div>
                <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                    </div>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值