Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention when using STRUCT in C

Tags:

c

I'm learning C and find someone defined a struct, its struct name has _ in front of it. This is my first time seen it, can someone tell me a little bit more about it? Why someone would use _aStructName instead of aStructName, what are the benefits?

struct _huffmanNode {     int value;     uint32_t frequency;      int hasChild;     struct _huffmanNode *child[2];      struct _huffmanNode *next; }; 

similarly I find someone using this kind of naming convention in the following code:

typedef struct HuffCode_ {  unsigned char      used; unsigned short     code; unsigned char      size;  } HuffCode; 
like image 694
Dean Avatar asked Apr 05 '11 21:04

Dean


People also ask

How do you name a struct?

The struct name suffix should be _s , and typedef struct is _st. Basic type aliase typedef suffix should be _kt , like typedef unsigned bits32_kt; Function typedef suffix should be _ft , prefer no *, e.g. typedef int (cnter_ft)(int cnt);

Should struct name be capitalized?

Function, typedef, and variable names, as well as struct, union, and enum tag names should be in lower case.

Does C use camelCase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.


2 Answers

There is no benefit in user code, it's just ugly. In the second example, the HuffCode_ isn't even necessary since the struct type is already named by the typedef.

The only places where this can be useful are:

  1. When StructName is already in use, StructName_ gives a different name (but you should really come up with a better name).
  2. Identifiers in the C standard library that are not defined by the standard shouldn't conflict with user code identifiers. Therefore, C library writers use the _ prefix in the hopes that users will not use that. Unfortunately, some users do.
  3. In very old compilers, it may be useful to give the struct a different name than is used in the typedef. You need both the typedef and the other name if you're building a linked structure (example).
like image 180
Fred Foo Avatar answered Sep 29 '22 02:09

Fred Foo


I think this is done mostly because of the very mistaken idea that a struct and a type cannot have the same name. In other words, that somehow

struct MyStruct; typedef struct MyStruct MyStruct; 

will collide in strange ways because the struct and the typedef have the same name. This is wrong in C. The name of a struct is considered a tag, whereas a typedef creates a type name. These live in two different namespaces and will not collide. In my opinion, it makes a lot more sense for the tag of the struct to be the same as the typedef, assuming you use a typedef at all. In C, you must always reference a struct with the struct keyword. For example, if you define

struct MyStruct;

but do not make a typedef, then the following is invalid:

void myfunction() {   MyStruct var;   /* compiler error: MyStruct is not defined. */   struct MyStruct var2; /* this is OK, MyStruct is a valid name for a struct. */ } 

If you want to define variables (or arguments) of type MyStruct in C, you must provide a typedef:

typedef struct MyStruct MyStruct;  void myfunction2() {   MyStruct var;  /* this is ok now */   struct MyStruct var2; /* this is still ok too */ } 

In this second example, var, and var2 have the same type, although this isn't obvious. Indeed, if the typedef were changed, they would no longer have the same type. Beware of this! It can cause some interesting bugs if type definitions change.

In C++, a struct definition essentially creates an implicit typedef so that both of the above code snippets compile. There is, in C++, essentially no difference between a struct name (or tag) and a type name. Which is, in my opinion, another great reason to name the two the same way, especially if you anticipate that your C module might be used by some C++ code at some point.

like image 35
Jeremy West Avatar answered Sep 29 '22 03:09

Jeremy West