1

I'm having issues trying to call a function where I pass in pointers to two structs.

Please could someone explain why I can't do this...

// in types.h

typedef struct {

    uint16_t size;
    uint16_t width;
    uint16_t height;
    void (*fn_init)(void);

} display_t, *display_ptr;

typedef struct {

    uint16_t size;
    const display_ptr display;

} context_t, *context_ptr;

// in main.c

void init_context(context_ptr context, const display_ptr display) {
    context->size = sizeof(context_t);
    context->display = display;
}

const display_t g_display= {
    0,
    400,
    800,
    NULL
};

context_t g_context;

void main(void) {

    // other code here...

    init_context(&g_context, &g_display);

} 

When I call the init_context function, I get the following error:

"argument of type "display_t const *" is incompatible with parameter of type struct <unnamed> *"

However, if I alter the declaration of the init_context to be as follows:

void init_context(context_ptr context, const display_t *display);

It works!

What am I missing or not understanding?

Thanks in advance! Kev

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
weblar83
  • 681
  • 11
  • 32
  • You're trying to modify `context->display` but it's defined as `const`. – Alex Díaz Mar 29 '15 at 17:08
  • 3
    There is a difference between `const Foo*` and `Foo * const`. One is a regular pointer to a constant Foo, the other is a constant pointer to a regular Foo. Your problem is that typedef'd pointer types work counterintuitively with const. It's best not to typedef pointer types. – n. m. could be an AI Mar 29 '15 at 17:10
  • Alejandro - Thanks but that isn't the cause of error. – weblar83 Mar 29 '15 at 17:10
  • Fixed. Removed the pointer declaration from the typedef struct and added "typedef const display_t* display_ptr;" which now works. Thanks @n.m. – weblar83 Mar 29 '15 at 17:21
  • [Is it a good idea to `typedef` pointers](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Jonathan Leffler Mar 30 '15 at 04:03
  • Once solved, please post the answere and mark as solved. – tswaehn Jun 10 '15 at 19:43

0 Answers0