0

I've tried every possible combination of *'s and &'s. I can't figure out what is wrong with this program and what is causing this compile time error

I'm getting this error:

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

With this code:

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

The function prototype is:

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

The function is empty, I'm just trying to get it to compile with an input file.

4
  • You call function() but only show a prototype for decode_instruction(). Please show the code you're having trouble with. Commented Sep 29, 2017 at 1:04
  • Additionally, you have to dereference a pointer to assign a value to the memory location it points to. Furthermore, if you do so without allocating memory first, that is undefined behavior. Why are your local variables in main() defined as pointers instead of variables? Commented Sep 29, 2017 at 1:05
  • Not only defined as pointers, but initialized with integers. You cannot dereference a pointer in an initializer...you can only initialize the pointer itself, then later can you assign through it (assuming you've given it memory). Commented Sep 29, 2017 at 1:09
  • 1
    Because you are initializing a pointer from an integer without a cast that would allow that to compile. (Sure, you have A cast, but it's not the cast the compiler is talking about) Commented Sep 29, 2017 at 2:03

2 Answers 2

6

These variables are pointers and you try to assign them integer (short) values:

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

That is why you get the compiler errors.

The question is not 100 % clear, but maybe this is what you want:

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);
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is you are initializing pointers with integer constants for a, p, q and s (ie, saying assign value 89 to type int* where 89 is of type int, not int*). You probably dont want those variables to be pointers, you can pass them in as pointers to those variables instead:

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

Without more information about decode_instruction, it is hard to tell if this is the correct form; it really does depend what that function does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.