【C语言】C tips

1. How to solve warning: cast to pointer from integer of different size and vice versa:

cpp 复制代码
#include <stdio.h>

int main() {
    void *p = NULL;
    int a = 3;
    p = (void *)a;
    a = (int)p;

    printf("%ld\n", p);
    printf("%ld\n", a);

    return 0;
}

The above code has the following warnings when compiling:

cpp 复制代码
/tmp/0pubXc3fmS.c: In function 'main':
/tmp/0pubXc3fmS.c:7:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
    7 |     p = (void *)a;
      |         ^
/tmp/0pubXc3fmS.c:8:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    8 |     a = (int)p;
      |         ^
/tmp/0pubXc3fmS.o

Converting pointer/data to (unsigned) long before casting to required data types can eliminate this warning: (intptr_t works the same)

cpp 复制代码
#include <stdio.h>

int main() {
    void *p = NULL;
    int a = 3;
    p = (void *)(long)a;
    a = (int)(long)p;

    printf("%ld\n", p);
    printf("%ld\n", a);

    return 0;
}

2.

相关推荐
kylezhao201941 分钟前
C#读取字节数组某个位的值
开发语言·c#
资生算法程序员_畅想家_剑魔1 小时前
Java常见技术分享-26-事务安全-锁机制-作用与分类
java·开发语言·数据库
Keep_Trying_Go1 小时前
基于无监督backbone无需训练的类别无关目标统计CountingDINO算法详解
人工智能·python·算法·多模态·目标统计
有时间要学习1 小时前
面试150——第三周
算法·面试
qq_406176141 小时前
JS 事件循环(Event Loop)
开发语言·前端·javascript
一车小面包1 小时前
Neo4j中的APOC
算法·neo4j
weixin_433179331 小时前
python - for循环,字符串,元组基础
开发语言·python
H_BB1 小时前
前缀和算法详解
数据结构·算法
聆风吟º2 小时前
【数据结构手札】时间复杂度详解:概念 | 大O渐进表示法 | 习题
数据结构·算法·时间复杂度·大o渐进表示法
智航GIS2 小时前
9.1 多线程入门
java·开发语言·python