【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.

相关推荐
Liangwei Lin1 小时前
LeetCode 74. 搜索二维矩阵
算法·leetcode·矩阵
phltxy1 小时前
Redis Hash 数据类型:详解命令与实战场景
redis·算法·哈希算法
辞旧 lekkk8 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
放羊郎8 小时前
基于ORB-SLAM2算法的优化工作
人工智能·算法·计算机视觉
mask哥8 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
2zcode8 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19979 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
如果'\'真能转义说9 小时前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
Aaswk10 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite10 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台