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

相关推荐
17´11 分钟前
Qt从入门到入土(八) -打包Qt程序
开发语言·c++·qt
AI+程序员在路上12 分钟前
QT显示网页控件QAxWidget、QWebEngineView及区别
开发语言·qt
南玖yy18 分钟前
C语言柔性数组深度解析:动态内存管理的艺术
c语言·开发语言·柔性数组
2301_7644413326 分钟前
python实现的生态模拟系统
开发语言·python·pygame
无世世27 分钟前
【Java从入门到起飞】面向对象编程(高级)
java·开发语言
q5673152337 分钟前
使用CPR库编写的爬虫程序
开发语言·爬虫·golang·音视频
星之卡比*38 分钟前
前端0基础---day18Math - Date - 定时器 (javascript)
开发语言·前端·javascript
星辰tsy43 分钟前
JS内存泄漏问题
开发语言·javascript
冠位观测者1 小时前
【Leetcode 每日一题 - 补卡】2070. 每一个查询的最大美丽值
数据结构·算法·leetcode
Martin_Yelvin1 小时前
记录一个Circle CI出现的错误
开发语言·前端·ci/cd