Why is struct tagName often different from typedef?

Sometimes I see this code (I hope that I remember it correctly):

typedef struct st {
    int a; char b;
} *stp;

Whereas a regular template that I'm familiar with looks like this:

typedef struct st {
    int a; char b;
} st;

So what is the advantage in the first code example?

+3
source share
5 answers

You probably mean this:

typedef struct ST {
  /* fields omitted */
} *STP;

An asterisk is at the end of the instruction. It simply means "define the STP type as a pointer to the structure of this type." The struct ( ST) tag is not needed, it is useful if you want to refer to the structure type again later.

You can also have both, for example:

typedef struct {
  /* fields omitted */
} ST, *STP;

This would allow us to use STfor designating the type of structure itself and STPfor pointers to ST.

typedefs, - (, ) , C ( ), . , , .

+6

, :

typedef struct{
   int a;
   char b;
} object, *objectPointer;

, () objectPointer struct (object), . struct . ,

objectPointer A = (objectPointer)malloc(sizeof(object));
A->a = 2;

A struct, , .

, objectPointer ,

struct object *A = (struct object *)malloc(sizeof(object));
A->a = 2;
, , objectPointer .
+2

, ,

+1

, typedef .

, , ,

struct tag v;

tag v;

, . , , C typedef, , , struct ( typedef C). , , typedef struct , , unix

struct stat;
int stat(const char*, struct stat*);

typedef. , (, ++ typedef, ++ , ).

0

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


All Articles