c - 初始化使指针来自整数而不进行强制转换

标签 c linux pointers casting compiler-errors

我已经尝试了 * 和 & 的所有可能的组合。我无法弄清楚这个程序出了什么问题以及是什么导致了这个编译时错误

我收到此错误:

error: initialization makes pointer from integer without a cast
compilation terminated due to -Wfatal-errors.

使用此代码:

int main() {
  unsigned int ten = (int) 10;
  unsigned short *a = (short) 89; 
  unsigned int *p =(int)  1;
  unsigned short *q = (short) 10;
  unsigned short *s = (short) 29;


  function(ten,a,p, q, s);

  return 0;
}

函数原型(prototype)为:

int function(unsigned int a, unsigned short *const b,
                   unsigned int *const c,
                   unsigned short *const d,
                   unsigned short *const e);

该函数是空的,我只是想用输入文件来编译它。

最佳答案

这些变量是指针,您尝试为它们分配整数(短)值:

unsigned short *a = (short) 89; 
unsigned int *p =(int)  1;
unsigned short *q = (short) 10;
unsigned short *s = (short) 29;

这就是您收到编译器错误的原因。

问题不是 100% 清楚,但也许这就是您想要的:

unsigned int ten = (int) 10;
unsigned short a = (short) 89; 
unsigned int p =(int)  1;
unsigned short q = (short) 10;
unsigned short s = (short) 29;

function(ten, &a, &p, &q, &s);

关于c - 初始化使指针来自整数而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46480290/

相关文章:

c - %p 在 printf 中的用法?

c - 使用 C 在终端中以 < 形式读取文件字节

linux - 给定两个目录树如何找到相同的文件?

c++ - 指针问题 - 获取访问冲突

当一个参数不变时更改 C 中函数指针的签名

c++ - 如何清除c中的内存

c++ - 双指针问题error passing through functions

objective-c - 在 Objective-C 中,如何使用 object_setIvar 设置原始值?

linux - 使用 ; 在 Lua 中连接问题

java - 使用 Java SSLSocket 时 Windows 和 Linux/OS X 之间有什么区别?