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

相关推荐
生成论实验室13 分钟前
《源·觉·知·行·事·物:生成论视域下的统一认知语法》第五章 事:行在时空中的具体化
人工智能·算法·架构·知识图谱·创业创新
炸膛坦客23 分钟前
嵌入式 - 数据结构与算法:(1-4)数据结构 - 单链表的两个核心缺点(引入循环/双向链表)
c语言·数据结构·链表
Liangwei Lin24 分钟前
LeetCode 283. 移动零
算法
2501_9327502627 分钟前
Java IO流基础全面详解:字节流、字符流
java·开发语言
冰暮流星34 分钟前
javascript之默认事件
开发语言·javascript·ecmascript
fengci.37 分钟前
CTF+随机困难题目
android·开发语言·前端·学习·php
l1t39 分钟前
DeepSeek总结的Python 3.14.5 发布候选版本
开发语言·python
雪度娃娃43 分钟前
设计模式——单例模式
开发语言·c++·设计模式
Cyber4K44 分钟前
【Python专项】进阶语法-日志分类与分析(2)
开发语言·前端·python
lbb 小魔仙1 小时前
Python + LangChain 环境搭建完全指南:从零构建本地 RAG 知识库(附 Ollama 本地模型集成)
开发语言·python·langchain