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

相关推荐
小O的算法实验室20 分钟前
2025年IEEE TITS,基于矩阵的进化计算+面向无线传感器网络数据收集无人机路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
kongba00722 分钟前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
OidEncoder28 分钟前
编码器分辨率与机械精度的关系
人工智能·算法·机器人·自动化
froginwe1137 分钟前
C 语言测验
开发语言
memcpy039 分钟前
LeetCode 2615. 等值距离和【相同元素分组+前缀和;考虑距离和的增量】中等
算法·leetcode·职场和发展
今夕资源网1 小时前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
炽烈小老头1 小时前
【 每天学习一点算法 2026/04/22】四数相加 II
学习·算法
alphaTao1 小时前
LeetCode 每日一题 2026/4/20-2026/4/26
算法·leetcode·职场和发展
三品吉他手会点灯1 小时前
C语言学习笔记 - 14.C编程预备计算机专业知识 - 本讲内容概述
c语言·笔记·学习
机器视觉知识推荐、就业指导1 小时前
Qt:真正的门槛不是入门,而是维护
开发语言·qt