0

In the following code, I thought saCmdQueue_s is a struct and saCmdQueue_t is an object of type saCmdQueue_s. But then I see the last line looks like they are using saCmdQueue_t to set the type for an object named sa_queue[SA_QSIZE]. Can some help me understand.

typedef struct saCmdQueue_s {

    uint8_t *buf;

    int len;

} saCmdQueue_t;

#define SA_QSIZE 6 // 1 heartbeat (GetSettings) + 2 commands + 1 slack

static saCmdQueue_t sa_queue[SA_QSIZE];
M.M
  • 138,810
  • 21
  • 208
  • 365

1 Answers1

0

In C++, when struct is declared, usage is different according to declare type. If struct is declared as typedef type, you can write other name at the end of struct. Other name is the new name of the type - struct you declared. Otherwise, if struct is declared as usual type, the result is the following.

struct saCmdQueue_s {
    uint8_t* buf;
    int len;
} saCmdQueue_t;

#define SA_QSIZE 6 // 1 heartbeat (GetSettings) + 2 commands + 1 slack

saCmdQueue_t sa_queue[SA_QSIZE];

Error: variable "saCmdQueue_t" is not a type name

If you write typedef in front of struct, the struct is declared the type like data type(int, char, string, vector, ...).

Sniper Dev
  • 44
  • 4
  • So when I use typedef struct the sa_CmdQueue_t is an alias for saCmdQueue_s? I read in my C++ book that the suffix _t is commonly used for the aliases of typedefs. – Jason Keiter Sep 01 '22 at 14:34