Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct x vs x_t in C

Tags:

c

struct

In my recent operating systems class we have a bunch of objects defined as such:

typedef struct someobj {
  ... stuff ...
} someobj_t;

I know what that does just fine.

The question is that sometimes in the given support code the structs were refered to as struct someobj *some, and sometimes as someobj_t *some. Is there an actual / useful reason to refer to structs in these two different ways, or is just a stylistic difference?

like image 225
Donnie Avatar asked Dec 08 '10 00:12

Donnie


2 Answers

While it's up to you whether you use a typedef or the struct name, there is a very good reason not to use typedef names ending in _t. All such names are reserved by POSIX for use by the implementation, and may refer to implementation-specific types or may be standardized in future versions of POSIX. Unfortunately many library authors ignore this.

Personally I prefer not to use typedef for structures, but if you do choose to use it, at least avoid the reserved namespace.

like image 127
R.. GitHub STOP HELPING ICE Avatar answered Nov 03 '22 01:11

R.. GitHub STOP HELPING ICE


Assuming we're asking about the following code (there's no typedef in the question as I'm writing, but I'm assuming it was meant to be there):

typedef struct someobj {
    // content
} someobj_t;

Actually usually it'd be sufficient to omit the name someobj and use someobj_t consistently. However there's that situation when you want the struct to refer to itself:

typedef struct someobj {
    struct someobj* next;
    // cannot say someobj_t* yet - typedef not complete
} someobj_t;

// and from now on, both struct someobj and`someobj_t are equivalent
like image 43
Kos Avatar answered Nov 03 '22 01:11

Kos