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

相关推荐
记得早睡~几秒前
leetcode654-最大二叉树
javascript·数据结构·算法·leetcode
go54631584653 分钟前
简单的 Python 示例,用于生成电影解说视频的第一人称独白解说文案
开发语言·python
vvilkim6 分钟前
使用 JavaScript 和 HTML5 实现强大的表单验证
开发语言·javascript·html5
旧厂街小江6 分钟前
LeetCode 第63题:不同路径 II
算法·程序员·架构
写代码的橘子n20 分钟前
unordered_set 的常用函数
数据结构·算法·哈希算法
EnigmaCoder31 分钟前
蓝桥杯刷题周计划(第二周)
学习·算法·蓝桥杯
森焱森35 分钟前
AArch64架构及其编译器
linux·c语言·单片机·架构
黑金IT35 分钟前
深入理解人脸特征向量及图片转换方法与开发架构
算法·架构
程高兴1 小时前
中性点不接地系统单相接地故障Matlab仿真
开发语言·matlab
HP-Patience1 小时前
决策树 vs 神经网络:何时使用?
神经网络·算法·决策树