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

相关推荐
闻缺陷则喜何志丹19 小时前
【状态压缩动态规划】P8733 [蓝桥杯 2020 国 C] 状态压缩动态规划|普及+
c++·算法·蓝桥杯·动态规划·洛谷
YGGP19 小时前
【Golang】LeetCode 189. 轮转数组
开发语言·leetcode·golang
Tisfy19 小时前
LeetCode 3379.转换数组:下标取模
算法·leetcode·题解·模拟·取模
Web打印19 小时前
Phpask(php集成环境)之01安装Apache
开发语言·php·apache
Zachery Pole19 小时前
JAVA_07_面向对象
java·开发语言
dc_001219 小时前
Java进阶——IO 流
java·开发语言·python
沐知全栈开发19 小时前
DOM 遍历
开发语言
桂花很香,旭很美19 小时前
[7天实战入门Go语言后端] Day 3:项目结构与配置——目录组织、环境变量与 viper
开发语言·数据库·golang
Lun3866buzha19 小时前
内窥镜设备部件检测与识别——基于Mask R-CNN的改进模型训练与实现
开发语言·r语言·cnn
骇城迷影19 小时前
代码随想录:数组篇
算法·leetcode