-1
typedef void* (*_add_elem)(shm_ds_t *ds, void *key, void *value, int size);
typedef void* (*_lookup)(shm_ds_t *ds, void *key);
typedef void  (*_dump)(shm_ds_t *ds);
typedef int   (*_compare)(void *key1, void *key2);

typedef struct shm_ds{
           void *ds;
           _add_elem shm_add_elem;
           _lookup  shm_lookup;
           _dump dump;
           _compare compare;
} shm_ds_t;

Its chicken and egg problem. I have defined some callback fn pointers which uses shm_ds_t type, but this type is defined after callback definitions. Similarly, if i change the order, then it reports the same issue with callback definitions which shall be used before defined in this case. Can anyone pls suggest the soln of this ?

masoud
  • 55,379
  • 16
  • 141
  • 208
Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44
  • 1
    why don't you add `typedef struct shm_ds shm_ds_t`? – Sourav Ghosh Sep 18 '16 at 12:15
  • OT: The suffix `_t` is reserved by POSIX. So to stay compatible to POSIX make up a different naming scheme for types. – alk Sep 18 '16 at 12:20
  • @Sourav Ghosh, after applying your suggestion i get this compilation error: note: expected ‘void * (*)(struct shm_ds *, void *, void *, int)’ but argument is of type ‘void * (*)(struct shm_ds_t *, void *, void *, int)’ it is distinguishing between `struct shm_ds` and `struct shm_ds_t` – Abhishek Sagar Sep 18 '16 at 12:24
  • 1
    It's `shm_ds_t` not `struct shm_ds_t`. – alk Sep 18 '16 at 12:26
  • but compiler is complaining about later. – Abhishek Sagar Sep 18 '16 at 12:27
  • the prototype of callbacks becomes `void * (*)(struct shm_ds *, void *, void *, int)` instead of void * (*)(shm_ds_t *, void *, void *, int) – Abhishek Sagar Sep 18 '16 at 12:28

1 Answers1

0

You need to define the type before the callbacks.

Add

typedef struct shm_ds shm_ds_t;

before the callbacks and later, use

struct shm_ds{
           void *ds;
           _add_elem shm_add_elem;
           _lookup  shm_lookup;
           _dump dump;
           _compare compare;
};

and you should be good to go.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • in other file, i am seeing this error : shm_ds->shm_add_elem = shm_add_elem; where shm_add_elem is a pointer to function `void * shm_ll_add_elem(shm_ds_t *ds, void *key, void *value, int size);` error : ./LinkedList/./../shm_ds.h:21:1: note: expected ‘void * (*)(struct shm_ds *, void *, void *, int)’ but argument is of type ‘void * (*)(struct shm_ds_t *, void *, void *, int)’ – Abhishek Sagar Sep 18 '16 at 12:30
  • @AbhishekSagar Show how you call them. – Sourav Ghosh Sep 18 '16 at 12:33
  • @AbhishekSagar: "*`but argument is of type ‘void * ()(struct shm_ds_t *, ...`*" again: `struct shm_ds_t ` is *not* a valid type. Use `shm_ds_t` instead. – alk Sep 18 '16 at 12:35
  • In Main, i am doing the following : shm_ds->shm_add_elem = shm_add_elem; where `shm_add_elem` is a pointer to the function `void * shm_ll_add_elem(shm_ds_t *ds, void *key, void *value, int size)`. – Abhishek Sagar Sep 18 '16 at 12:38