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

相关推荐
智驱力人工智能5 分钟前
基于视觉分析的人脸联动使用手机检测系统 智能安全管理新突破 人脸与手机行为联动检测 多模态融合人脸与手机行为分析模型
算法·安全·目标检测·计算机视觉·智能手机·视觉检测·边缘计算
悟能不能悟32 分钟前
java的java.sql.Date和java.util.Date的区别,应该怎么使用
java·开发语言
2301_7644413339 分钟前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天39 分钟前
3.4、Python-集合
开发语言·笔记·python·学习·算法
_院长大人_2 小时前
设计模式-工厂模式
java·开发语言·设计模式
MATLAB代码顾问2 小时前
MATLAB实现决策树数值预测
开发语言·决策树·matlab
priority_key3 小时前
排序算法:堆排序、快速排序、归并排序
java·后端·算法·排序算法·归并排序·堆排序·快速排序
不染尘.4 小时前
2025_11_7_刷题
开发语言·c++·vscode·算法
ben9518chen4 小时前
嵌入式Linux C语言程序设计九
linux·c语言
似水এ᭄往昔4 小时前
【C++】--stack和queue
开发语言·c++