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

相关推荐
冯诺依曼的锦鲤5 分钟前
从零实现高并发内存池:TCMalloc 核心架构拆解
c++·学习·算法·架构
Thomas_Lee_OR8 分钟前
多Agent路径规划 LaCAM for multi-agent path finding (MAPF)
算法·路径规划·仓储机器人·mapf
一切皆是因缘际会16 分钟前
可落地数字生命工程:从记忆厮杀到自我意识觉醒全链路,AGI内生智能硅基生命心智建模
人工智能·深度学习·算法·机器学习·ai·系统架构·agi
nlpming27 分钟前
opencode Agent 详解
算法
江南十四行31 分钟前
排序算法进阶:直接插入排序(简单排序)与希尔排序
数据结构·算法·排序算法
nlpming31 分钟前
opencode System Prompt 构建机制 & AGENTS.md注入机制
算法
nlpming32 分钟前
opencode - 安装和配置
算法
爱滑雪的码农34 分钟前
Java基础十一 流(Stream)、文件(File)和IO
java·开发语言·python
nlpming36 分钟前
opencode 内置工具
算法
叶小鸡36 分钟前
Java 篇-项目实战-天机学堂(从0到1)-day11
java·开发语言