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

相关推荐
IpdataCloud3 分钟前
IP查询工具的准确率怎么评估?一份可上生产的选型与验收指南
网络·人工智能·算法
生信研究猿12 分钟前
leetcode 78.子集
算法·leetcode·深度优先
龙俊杰的读书笔记15 分钟前
一文读懂python并发&并行编程--以xinference框架应用为例
开发语言·网络·python
sycmancia17 分钟前
Qt——文本编辑器中的功能交互
qt·算法
dollmarker18 分钟前
vulnhub靶场之hacksudo: 2 (HackDudo)靶机-NFS提权
c语言·网络·网络安全·php
liulilittle18 分钟前
递归复制搜索所有的lua文件到指定目录
java·开发语言·lua·cmd
Gofarlic_oms129 分钟前
Allegro高级功能模块许可证管理注意事项
运维·服务器·开发语言·matlab·负载均衡
启山智软32 分钟前
前沿主流技术栈商城系统(Java JDK21 + Vue3 + Uniapp)
java·开发语言·uni-app
浅念-35 分钟前
分治算法专题|LeetCode高频经典题目详细题解
数据结构·c++·算法·leetcode·职场和发展·排序·分治
Magic-Yuan39 分钟前
算力的迷雾
人工智能·算法·机器学习