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

相关推荐
逸风尊者22 分钟前
XGBoost模型工程使用
java·后端·算法
LUVK_31 分钟前
第七章查找
数据结构·c++·考研·算法·408
lhbian34 分钟前
AI编程革命:Codex让脚本开发提速10倍
开发语言·汇编·jvm·c#
jiayong2335 分钟前
第 36 课:任务详情抽屉快捷改状态
开发语言·前端·javascript·vue.js·学习
khalil102037 分钟前
代码随想录算法训练营Day-31贪心算法 | 56. 合并区间、738. 单调递增的数字、968. 监控二叉树
数据结构·c++·算法·leetcode·贪心算法·二叉树·递归
FFF_6345602340 分钟前
通用 vue 页面 js 下载任何文件的方法
开发语言·前端·javascript
阿奇__1 小时前
uniapp支付宝 H5 开发踩坑,hash模式下取参要规范!
开发语言·uni-app
eggwyw1 小时前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
一行代码一行诗++1 小时前
C语言中scanf详解
c语言·开发语言
lihihi1 小时前
P9936 [NFLSPC #6] 等差数列
算法