初始化使得整数指针没有强制转换

时间:2017-09-29 01:00:53

标签: c linux pointers casting compiler-errors

我已经尝试了*和's'的所有可能组合。我无法弄清楚这个程序有什么问题以及造成这个编译时错误的原因

我收到了这个错误:

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;
}

函数原型是:

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

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

2 个答案:

答案 0 :(得分:5)

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

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);

答案 1 :(得分:1)

问题是您正在使用apqs的整数常量初始化指针(即,说明要赋值89来键入int*其中89的类型为int,而非int*。您可能不希望这些变量成为指针,您可以将它们作为指针传递给那些变量:

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;
}

如果没有关于decode_instruction的更多信息,很难说这是否是正确的形式;它确实取决于该功能的功能。