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

相关推荐
geovindu3 分钟前
java:Abstract Factory Pattern
java·开发语言·后端·设计模式·抽象工厂模式·创建型模式
geovindu6 分钟前
java: Factory Method Pattern
java·开发语言·后端·设计模式·工厂方法模式·创建型模式
z小猫不吃鱼14 分钟前
模型剪枝经典论文精读:Channel Pruning for Accelerating Very Deep Neural Networks
算法·机器学习·剪枝
m0_5870989926 分钟前
Qt,二进制文件读写建议
开发语言·qt
njsgcs31 分钟前
已知链中心距用节数算中间电机摆放位置 常量弧长 三链轮弦长模型算法
算法
爱敲代码的小鱼31 分钟前
springsecurity:
java·开发语言·数据库
秋田君33 分钟前
QT_QT信号与槽机制
开发语言·数据库·qt
辞旧 lekkk34 分钟前
【Qt】 系统相关:事件与定时器
开发语言·qt·学习·萌新
Wang's Blog37 分钟前
Java框架快速入门: Spring Bean核心配置与实例化详解
java·开发语言·spring
三十岁老牛再出发1 小时前
07.07.每日总结
c语言·windows·python