Warning-cast-to-pointer-from-integer-of-different-size

59次阅读

共计 508 个字符,预计需要花费 2 分钟才能阅读完成。

报错:[Warning] cast to pointer from integer of different size [-Wint-to-pointer-cast]
中译:[警告] 把指针不同大小的整数 (-Wint-to-pointer-cast)

起源 :看到了知乎这个问题指针的指针定义为什么用 int * ptr,而不是 int ptr?,一下子没反应过来。借此机会在温习一下指针。


文章描述了这两种差异化的写法

int a;
int *b = &a;
int **c = &b;

int a;
int *b = &a;
int *c = &b;  // 请留意这里 

差别在第三行:第一种写法用的是二级指针,通俗易懂;第二种写法比较骚。


为了说明这个问题,我打算用这个图来说明

实验目的都是想通过 c 来访问 a,得到结果 1

1.1

代码

#include <stdio.h>                                
int main()                                         
{   
    int a=1;
    int *b = &a;
    int *c=b;  // 请留意这里
    
    printf("%d\n",*c);
}

输出
1

图示

1.2

代码

#include <stdio.h>                                
int main()                                         
{   
    int a=1;
    int *b = &a;
    int *c=(int*)&b;  // 请留意这里
    
    printf("%d\n",**(int**)c);
}

输出
1

图示

正文完
 0