Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing structs by pointer in C89

Tags:

c

c89

I am working with a C89 compiler and I'm coming across some pointer typing error.

Calling code:

struct cpu_state_type cpu_state;
//Stuff here....
foo()
{
    print_out_cpu(&cpu_state);
}

Print_out_cpu is defined elsewhere and the H file is #included in.

struct cpu_state_type
{
  int r[12];
};
void print_out_cpu(struct cpu_state_type *c);

I get error:

error: incompatible type for argument 1 of 'print_out_cpu'

As best as I can understand,&cpu_state returns type cpu_state_type*, so I am confused.

like image 304
Paul Nathan Avatar asked Feb 20 '26 16:02

Paul Nathan


1 Answers

Are you sure the prototype has the * in it? If I compile (gcc -std=c89) the following code, I get that exact error:

  struct cpu_state_type {
     int r[12];
  };

  // note that it is the structure as the param here (not the pointer)
  void print_out_cpu(struct cpu_state_type c);
  struct cpu_state_type cpu_state;

  foo()
  {
     print_out_cpu(&cpu_state);
  }
like image 109
Mark Wilkins Avatar answered Feb 23 '26 08:02

Mark Wilkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!