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

相关推荐
q***d1731 小时前
Rust在网络中的协议栈
开发语言·网络·rust
星释1 小时前
Rust 练习册 88:OCR Numbers与光学字符识别
开发语言·后端·rust
一生要强的ymy1 小时前
Polar PHP是世界上最好的语言(困难)
开发语言·php
我命由我123452 小时前
Java NIO 编程 - NIO Echo Server、NIO Client(NIO 异步客户端、NIO Selector 异步客户端)
java·开发语言·网络·java-ee·intellij-idea·intellij idea·nio
前端炒粉3 小时前
35.LRU 缓存
开发语言·javascript·数据结构·算法·缓存·js
星释4 小时前
Rust 练习册 75:ETL与数据转换
开发语言·rust·etl
happyjoey2174 小时前
使用Qt自带的Maintenance Tool将Qt6.9升级为QT6.10
开发语言·qt
断剑zou天涯5 小时前
【算法笔记】窗口内最大值或最小值的更新结构
java·笔记·算法
smj2302_796826525 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
骑着猪去兜风.7 小时前
线段树(二)
数据结构·算法