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

相关推荐
孙启超2 分钟前
【AI开发之Rust】第 13 课:async/await 与 tokio 异步运行时
开发语言·后端·rust
Am-Chestnuts13 分钟前
DeepSeek内容转Word有哪几种方法?用DS随心转对比6条主流路径
开发语言·人工智能
大黄说说14 分钟前
App 启动速度优化:冷启动从 3s 降到 800ms 完整实操
开发语言
Lazionr28 分钟前
多态:从多种形态到运行时绑定
开发语言·c++
tryxr34 分钟前
矩阵的几种基础变换
java·数据结构·算法·矩阵
老当益壮梁奶奶37 分钟前
ARM汇编学习笔记(六):【i.MX6ULL】时钟系统与定时器(EPIT & GPT)
c语言·arm开发·嵌入式硬件·学习·51单片机
mmmmath_338 分钟前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
大黄说说1 小时前
iOS 列表滑动卡顿:UITableView / UICollectionView 深度优化实战
开发语言
Selvaggia1 小时前
DMD(Distribution Matching Distillation,分布匹配蒸馏)
算法
于樱花森上飞舞1 小时前
【Redis】哨兵详解
java·开发语言·数据库·redis