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

相关推荐
Hunter_pcx1 分钟前
ubuntu:内存假泄漏
linux·运维·服务器·开发语言·c++·人工智能·ubuntu
想带你从多云到转晴1 分钟前
07、数据结构与算法---优先级队列(堆)与排序
java·数据结构·算法
三品吉他手会点灯9 分钟前
C语言学习笔记 - 36.数据类型 - 为什么需要输出控制符
c语言·开发语言·笔记·学习
吃好睡好便好11 分钟前
在Matlab中绘制非默认峰值图
开发语言·学习·算法·matlab
qq_4017004111 分钟前
Qt如何 发送带结构体数据的信号
开发语言·qt
NagatoYukee14 分钟前
Java 商品交易实验(第二版)
java·开发语言
阳光九叶草LXGZXJ25 分钟前
自制数据库迁移工具-C版-07-HappySunshineV1.6-(支持PG、达梦、Gbase8a)
linux·c语言·开发语言·数据库·学习·postgresql
不吃土豆的马铃薯25 分钟前
5.SGI STL 二级空间配置器 _S_chunk_alloc核心函数解析
开发语言·c++·vscode·c·内存池
学掌门26 分钟前
JavaScript:为什么命名参数比位置参数更好
开发语言·javascript·ecmascript
码界筑梦坊27 分钟前
124-基于Python的航空旅客满意度数据可视化分析系统
开发语言·python·信息可视化·数据分析·flask·毕业设计