-2
#include <stdio.h>

struct m_tag {
    short m_tag_id;
    short m_tag_len;
    int   m_tag_cookie;
};
struct packet_tags {
    struct m_tag *slh_first;
}tags;

#define SFIRST(head) ((head).slh_first)
int main(void) {
    printf("%p\n", SFIRST(&tags));
    return 0;
}

In function 'main': error: request for member 'slh_first' in something not a structure or union

what is the problem with this code?

Neha
  • 11
  • 2

2 Answers2

5

You are trying to access the slh_first member using operator. which requires an instance of packet_tags on the left side. But you are not passing an instance of packet_tags to SFIRST(), you are passing a pointer to a packet_tags instance. So, in order to access the member, you need to dereference the pointer to reach the instance. Calling operator. on a pointer is what the compiler is complaining about.

Change SFIRST() to this instead:

#define SFIRST(head) ((head)->slh_first)

Or, less preferred but just as valid:

#define SFIRST(head) ((*(head)).slh_first)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

Change your code to below code:

#include <stdio.h>

struct m_tag {
    short m_tag_id;
    short m_tag_len;
    int   m_tag_cookie;
};
struct packet_tags {
    struct m_tag *slh_first;
}tags;

#define SFIRST(head) ((head)->slh_first)
int main(void) {
    printf("%p\n", SFIRST(&tags));
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770