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

相关推荐
水云桐程序员6 小时前
C++可以写手机应用吗
开发语言·c++·智能手机
测试员周周7 小时前
【AI测试智能体】为什么传统测试方法对智能体失效?
开发语言·人工智能·python·功能测试·测试工具·单元测试·测试用例
平凡但不平庸的码农7 小时前
Go Slice 详解
算法·golang
RSTJ_16257 小时前
PYTHON+AI LLM DAY THREETY-NINE
开发语言·人工智能·python
赏金术士8 小时前
Kotlin 从入门到进阶 之函数模块(核心基础)(二)
android·开发语言·kotlin
加号310 小时前
【Qt】 应用程序发布:依赖库拷贝与部署指南
开发语言·qt
Jasmine_llq10 小时前
《B3867 [GESP202309 三级] 小杨的储蓄》
算法·循环遍历·数组累加(模拟)·索引定位·顺序输出
啦啦啦_999910 小时前
案例之 逻辑回归_电信用户流失预测
算法·机器学习·逻辑回归
('-')10 小时前
八股复习2:Java Array list和Linked list
java·开发语言
风筝在晴天搁浅10 小时前
快手/字节 CodeTop LeetCode 415.字符串相加
算法·leetcode