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

相关推荐
可靠的仙人掌7 分钟前
SAC(Soft Actor-Critic)算法底座
开发语言·算法·php
学逆向的29 分钟前
汇编——数据存储模式
开发语言·汇编·网络安全
geovindu38 分钟前
CSharp: Prototype Pattern
开发语言·后端·设计模式·.net·原型模式·创建型模式
海石1 小时前
单调栈复健,顺便,牺牲一下吧,空间复杂度!一切献给AC
算法·leetcode
海石1 小时前
JS击败94%,Hard题想不到动态规划,那就用数组和栈试试
算法·leetcode
天天进步20151 小时前
Python全栈项目--校园食堂点餐与推荐系统
开发语言·python
aaPIXa6222 小时前
C++模板元编程:编译期计算Fibonacci数列
java·开发语言·c++
eybk2 小时前
写一个可以编制pdf文件的python程序
开发语言·python·pdf
cui_ruicheng2 小时前
Python从入门到实战(八):封装、多态与抽象类
开发语言·python
apihz2 小时前
台风实时与历史详情查询免费 API 接口完整教程
android·开发语言·tcp/ip·dubbo·台风·天气预报