2

In a function with a single va_list arg, I'm trying to attribute a list of variables as per an externally defined (header file) struct wifi_ap_record_t (other values omitted for clarity).

The type definition in "wifi_ap_record_t_header_location.h" is as follows, this is an external library I'm unable to edit:

typedef struct {
    uint8_t ssid[33];                     /**< SSID of AP */

} wifi_ap_record_t;

On compilation I'm getting the following error: error: dereferencing pointer to incomplete type 'const struct wifi_ap_record_t' which is upset about the p->ssid pointer. I've tried redefining the typedef with and without the pointer as well as defining a blank wifi_ap_record variable of type wifi_ap_record_t.

Code as below, what is missing here?

#include "wifi_ap_record_t_header_location.h"

typedef struct wifi_ap_record_t* wifi_ap_record;

void function(va_list *ap) {
  const struct wifi_ap_record_t *p = va_arg(*ap, const struct wifi_ap_record *);
  printf("ssid: %s", p->ssid;
}
Alex Turner
  • 470
  • 1
  • 4
  • 14

3 Answers3

2

After the typedef in the external library, the name of the type is wifi_ap_record_t, not struct wifi_ap_record_t.

Names that start with struct is a separate space, but typedef-ed names are not in it, even if they refer to a struct type.

There should be no struct keyword in your own code. A pointer to this struct will have the type wifi_ap_record_t*, not struct wifi_ap_record_t*. You don't need your own typedef.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
2

this is an example why pointers should not be typedefed

  1. You have a bunch of another warnings, which you ignore and this is wrong.
  2. Do not hide pointers in typedefs - even if this 5 lines example you have already lost lost control what is the pointer and what is not (and you have ** pointer there).

This is much more clearer, readable and compiles OK

  wifi_ap_record_t *p = va_arg(*ap, wifi_ap_record_t *);
  printf("ssid: %s", p ->ssid);
0___________
  • 60,014
  • 4
  • 34
  • 74
0

Alex, define your structure like this:

struct wifi_ap_record_t{
    uint8_t ssid[33];                     /**< SSID of AP */

};

It seems like you have defined a structure but without any name. Also you have created a object out of that.

In case it is an external structure, check if that structure is defined under any other structure. Refer this question for more information.

Finally, as the other answer and comment says, you should remove the struct word while defining the object. So, it will become:

typedef wifi_ap_record_t* wifi_ap_record;

Similarly fix other such instances.

R4444
  • 2,016
  • 2
  • 19
  • 30
  • Unfortunately I can't as that's how it's defined in an external library – Alex Turner May 04 '19 at 05:55
  • 2
    @AlexTurner: "*I can't as that's how it's defined in an external library*" Then change this `const struct wifi_ap_record_t *p` to be `const wifi_ap_record_t *p`. – alk May 04 '19 at 06:21
  • 2
    "*`typedef wifi_ap_record_t* wifi_ap_record;`*" hiding a pointer-type behind a typedef mostly never is a good idea. – alk May 04 '19 at 06:28