Function pointer, struct as parameter

Once again today with renaming.

The structure has a pointer to a function, in this function I want to work with data from this structure, so the pointer to the structure is indicated as a parameter.

Demonstration of this problem

#include <stdio.h>
#include <stdlib.h>

struct tMYSTRUCTURE;

typedef struct{
    int myint;
    void (* pCallback)(struct tMYSTRUCTURE *mystructure);
}tMYSTRUCTURE;


void hello(struct tMYSTRUCTURE *mystructure){
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
}

int main(void) {
    tMYSTRUCTURE mystruct;
    mystruct.pCallback = hello;

    mystruct.pCallback(&mystruct);
    return EXIT_SUCCESS;

}

But I get a warning

.. \ src \ retyping.c: 31: 5: warning: passing argument 1 from 'mystruct.pCallback' from an incompatible pointer type .. \ src \ retyping.c: 31: 5: note: expected 'struct tMYSTRUCTURE *', but the argument is of type 'struct tMYSTRUCTURE *'

the expected 'structure tMYSTRUCTURE *', but this is 'struct tMYSTRUCTURE *', ridiculous!

any idea how to fix it?

+5
2

typedef , struct typedef 'd. Forward, struct, typedef, .

#include <stdio.h>
#include <stdlib.h>

struct tagMYSTRUCTURE;
typedef struct tagMYSTRUCTURE tMYSTRUCTURE;

struct tagMYSTRUCTURE {
    int myint;
    void (* pCallback)(tMYSTRUCTURE *mystructure);
};


void hello(tMYSTRUCTURE *mystructure){
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
}

int main(void) {
    tMYSTRUCTURE mystruct;
    mystruct.pCallback = hello;

    mystruct.pCallback(&mystruct);
    return EXIT_SUCCESS;

}
+5

:

#include <stdio.h>
#include <stdlib.h>

struct tMYSTRUCTURE_;

typedef struct tMYSTRUCTURE_ {
  int myint;
  void (* pCallback)(struct tMYSTRUCTURE_ *mystructure);
} tMYSTRUCTURE;


void hello(tMYSTRUCTURE *mystructure){
  puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
}

int main(void) {
  tMYSTRUCTURE mystruct;
  mystruct.pCallback = hello;

  mystruct.pCallback(&mystruct);
  return EXIT_SUCCESS;

}

struct typedef. , , ( ) ... , .

, GCC .

+2

All Articles