Typedef structural question

Why should I do this?

typedef struct Frame_s
{
int x;
int y;
int z;
} Frame_t;

Also, if I want to create an object, how do I use Frame_s or Frame_t?

+3
source share
6 answers

You would use Frame_t.

Since typedefyou say that Frame_t, and struct Frame_sare the same type .

So these are equivalent sentences:

// 1
Frame_t f;

// 2
struct Frame_s f;

I would use:

typedef struct
{
   int x;
   int y;
   int z;
} Frame_t;

And always declare my vars as follows:

Frame_t f1, f2, f3;

The confusion usually comes from the places where you use this sentence in a C ++ code snippet. If you use C ++ with this typedef, you can use either:

// 1
Frame_t f;

// 2
Frame_s f;

C, //2 .

+6

struct Frame_s, Frame_t.

typedef, typedefed name, Frame_t, struct , .

Frame_t, struct Frame_s, .

+2

typedef, , , , , . ,

typedef struct Frame Frame;

, Frame, , .

POSIX : "sys/stat.h": struct stat stat:

int stat(const char *path, struct stat *buf);
+1

, struct Frame_s foo, Frame_t foo ( , typedefing). , Frame_s , Frame_t - , Frame.

0

typedef . , , , -

struct Frame_s *function_name()

. .. typedef -

Frame_t *function_name()

! ...

0

, :

struct Frame_s f;
Frame_t f;

In fact, you can leave Frame_sout of the ad, because it is not needed.

typedef struct
{
  int x;
  int y;
  int z;
} Frame_t;
0
source

Source: https://habr.com/ru/post/1767583/


All Articles