Passing structures with a pointer in C89

I am working with a C89 compiler and I am encountering a pointer input error.

Call Code:

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

Print_out_cpu is defined elsewhere, and file H is included.

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

I get an error:

error: incompatible type for argument 1 of 'print_out_cpu'

As far as I understand, &cpu_statetype returns cpu_state_type*, so I'm confused.

+3
source share
2 answers

Are you sure the prototype has it *? If I compile ( gcc -std=c89) the following code, I get this 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);
  }
+1
source

I don't see any problems, so I wonder if this is a mistake in your include statement or in a file, etc.

, . , :

#include struct cpu_state_type cpu_state;

void foo() {
    print_out_cpu (& cpu_state); }

, , . , ( #include) .

0

Source: https://habr.com/ru/post/1736178/


All Articles