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

相关推荐
梅菲塔亚几秒前
华为云从入门到实战
开发语言·华为云·php
霸道流氓气质5 分钟前
幂等性与 Upsert:防止重复操作产生副作用的技术
开发语言·php
地平线开发者5 分钟前
地平线征程6M YOLOv8s 部署
算法·自动驾驶
Run_Teenage8 分钟前
算法:Manacher(马拉车)算法、中心扩展算法
算法
牛奔17 分钟前
mise 管理多版本 Go 项目
开发语言·前端·chrome·后端·golang
波力海苔夹心脆67530 分钟前
C# 视觉检测实战:PLC 按钮触发 VisionPro 检测,OK 亮灯 / NG 灭灯(西门子 PLC,含界面显示与图片存档)
开发语言·经验分享·c#·自动化·视觉检测·.net
2601_9628857243 分钟前
如何用 Python 计算 BBI 多空指标?
开发语言·python
Tisfy1 小时前
LeetCode 0301.删除无效的括号:二进制枚举 / 回溯
算法·leetcode·字符串·题解·回溯·括号匹配·二进制枚举
Nil2081 小时前
leetcode 322零钱兑换
算法·leetcode·职场和发展
老王爱玩车1 小时前
动态内存管理
c语言·开发语言·学习·面试