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

相关推荐
羊小蜜.13 分钟前
Mysql 03: 连接查询全解——内连接、外连接与复合条件查询
数据库·mysql·算法·连接查询
_Twink1e14 分钟前
[算法竞赛]九、C++标准模板库STL常用容器大全
开发语言·c++
vivo互联网技术22 分钟前
CVPR 2026 | C²FG:用分数差异分析提高条件生成中CFG的引导
人工智能·算法·aigc
永恒_顺其自然26 分钟前
Java Web 传统项目异步分块上传系统实现方案
java·开发语言·前端
爱编码的小八嘎35 分钟前
C语言完美演绎6-11
c语言
bu_shuo41 分钟前
c++中对数组求和
开发语言·c++
赫瑞41 分钟前
Java中的大数处理 —— BigInteger
java·开发语言
r_oo_ki_e_42 分钟前
java25--Collection集合
java·开发语言
elseif1231 小时前
【Markdown】指南(上)
linux·开发语言·前端·javascript·c++·笔记
初九之潜龙勿用1 小时前
C# 解决“因为算法不同,客户端和服务器无法通信”的问题
服务器·开发语言·网络协议·网络安全·c#