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

相关推荐
leaves falling20 分钟前
C/C++ 的内存管理,函数栈帧详讲
java·c语言·c++
文静小土豆24 分钟前
Java 应用上 K8s 全指南:从部署到治理的生产级实践
java·开发语言·kubernetes
西西弗Sisyphus32 分钟前
Python 在终端里彩色打印
开发语言·python·print·彩色打印
浅念-1 小时前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客
CoovallyAIHub1 小时前
无人机拍叶片→AI找缺陷:CEA-DETR改进RT-DETR做风电叶片表面缺陷检测,mAP50达89.4%
算法·架构·github
Rsun045511 小时前
3、Java 工厂方法模式从入门到实战
java·开发语言·工厂方法模式
wjs20241 小时前
C++ 基本的输入输出
开发语言
CoovallyAIHub1 小时前
混合训练反而更差?VLM Agent在训练前协调跨数据集标注,文档布局检测F-score从0.860提升至0.883
算法·架构·github
鸿途优学-UU教育1 小时前
教材质量——法考培训的根基与底气
算法
_深海凉_1 小时前
LeetCode热题100-最大数(179)
算法·leetcode·职场和发展