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

相关推荐
qq_25183645711 分钟前
基于java Web 动漫视频网站毕业论文
java·开发语言·前端
野生风长13 分钟前
C++入门基础:从命名空间到引用与指针的全面解析
开发语言·c++
来一碗刘肉面24 分钟前
栈的应用(表达式求值)
数据结构·算法
FoldWinCard26 分钟前
D6 Python 基础语法 --- 保留关键字
开发语言·python
xiaowang1234shs1 小时前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio
小果因子实验室1 小时前
量化研究--策略迁移算法1研究
算法
风吹心凉2 小时前
python3基础2026.7.22
开发语言·python
qq_452396232 小时前
第五篇:《接口与错误处理:Go 的哲学》
开发语言·后端·golang
Escalating_xu2 小时前
C++可变参数模板与引用折叠精讲
开发语言·c++
Web极客码2 小时前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习