Probably some C-shnye tricks. Like their classes. It seems to be an ad here already. Maybe that's why? Here's a line like this (from OpenCV):

typedef struct CvFileStorage CvFileStorage; 

Why so to write? Not found in any way, immediately, the answer to this. Once (a few years ago), I remember reading about it. Then pure C was probably in fashion, but now it is forgotten. I saw something similar in the code from Windows CE6, etc. I remembered this :), but I don’t remember what I need to do. If any more. pieces of code need to lead, then tell me. This line is in front of a structure that somewhere in the field uses the type from this typedef. But why write so, and not just declare it? Thank.

  • 2
    Possible duplicate question: Why do I need typedef? - αλεχολυτ
  • @alexolut, like different questions? - Qwertiy
  • @Qwertiy what? In fact, everything explains the possibility of using a type without mentioning a struct. The fact that the names match is just a special case. - αλεχολυτ
  • Probably the most typedef demand for declaring pointers to functions (especially returning functions too -)) / And here it looks like some kind of imitation of languages ​​in which you can actually declare your types. - avp

3 answers 3

There are at least two good reasons to declare this typedef.

 typedef struct CvFileStorage CvFileStorage; 

The first reason is that in C programs you must specify the keywords struct or enum before the name of the structure or enumeration. It looks burdensome when entering code. Very often programmers forget to specify these words, which leads to a compilation error. Therefore, this typedef simplifies the life of programmers by allowing them not to write these keywords before the name of the structure or enumeration.

The second reason is that the names of the structures and other identifiers are in different namespaces. Therefore, the same name can be used to declare a structure and a regular variable. For example, the following code snippet is valid.

 struct CvFileStorage { //... }; int CvFileStorage; 

This code snippet declares a structure named CvFileStorage and an int variable with the same name. These declarations do not conflict with each other, because, as has already been written, the struct keyword must be preceded by the structure name.

In C you can write, for example

 struct CvFileStorage { int CvFileStorage; } CvFileStorage; 

This declaration is valid because these three matching identifiers are in different namespaces.

However, this may be confusing for those who read the code, because if the programmer mistakenly omits the struct keyword before the structure name, it may still be correct from the point of view of the language syntax, although the structure was actually meant not a variable with the same name.

For example, in this expression, the carelessness programmer forgot to specify the struct keyword, and nevertheless received the correct expression, since there is a variable with the same name

 sizeof( CvFileStorage ) 

To avoid such confusion, it is also advisable to reserve this name without the struct keyword for the structure name using typedef ..

In C ++, the struct keyword can be omitted when referring to a structure. Then the question arises: what about the fact that in C you can declare a variable or function with the same name as the structure name?

This question is solved as follows: the name of a variable or the name of a function hides the declaration of a structure with the same name. Therefore, when referring to the structure, you must specify the specified name.

For example,

 struct CvFileStorage { //... }; void CvFileStorage(); 

In this code fragment, the function declaration hides the declaration of the structure of the same name. Therefore, if, for example, you want to declare an object of this structure, then you will need to specify the qualified name of the structure.

 struct CvFileStorage obj; 

Or, for example, you can write these ads.

 struct CvFileStorage { //... }; void CvFileStorage( struct CvFileStorage ); 

These names will not conflict with each other, since the structure uses its qualified name.

  • Damn ... Divine! This is the explanation after all. (!) They (OpenCV) are exactly the structure (as a predicate is obtained) and the function with the same name (see below). And on them I "stuck" in the ideas of the strong. Here they are (very possibly there are others, but I don’t know about them): - AleksBak
  • Something I can not add code about this - the structure is the following struct RoiPredicate {RoiPredicate (const Rect & _r): r (_r) {} bool operator () (const KeyPoint & keyPt) const {return! R.contains (keyPt.pt) ; } Rect r; }; (of features2d / src / feature2d.cpp) and there is also a feature with the same name RoiPredicate. Now there is something else to think again. Thank. - AleksBak

In c ++, if you need to declare a structure type variable, you just need to write the type name and the variable. In pure si it is not. And you should always write a struct. This is all because the types for structures and other types are located in different scopes. But since programmers are lazy creatures, it is better to write one

 typedef struct CvFileStorage CvFileStorage; 

and then do not think about why it is not compiled (because they forgot the struct) or why it works as something strange (because they forgot the struct, and someone declared their type not a structure with the same name).

But often they write more interesting:

 typedef struct { // тут объявление структры } struct_name; 

In this case, everything is created immediately as usual in the usual ++.

  • And honestly, at least 10 times thanks to you, if that is possible. Because now I also began to think from the perspective of C vs C ++ when disassembling their header (this is from core // types_c.h) it’s not for nothing that there are verification directives #if cplusplus (or how exactly its syntax will be - it doesn’t matter here). I just write code using parts of theirs and came across this typedef. Thanks again. - AleksBak
  • 2
    many are surprised to learn that with and with ++, although very similar, but in fact two different languages. And if you need to provide two languages ​​at the same time, sometimes you need to watch very carefully (the classic is the value of sizeof('a') ). And 10-fold is just a tick next to votes “for” :) - KoVadim

Without this typedef , any mention of the CvFileStorage structure CvFileStorage be written in full:

 struct CvFileStorage cv; void func(struct CvFileStorage* cv); 

and so on. If there is a typedef declaration, the word struct can be thrown:

 CvFileStorage cv; void func(CvFileStorage* cv); 
  • Thank you very much. - AleksBak
  • @AleksBak thanks, they are valued in the form of such green ticks ... :) - Pavel Mayorov
  • What are some green ticks? The arrows see the bottom / top of the numbers I see, but they are gray and after clicking on the top it became orange. This is probably the flood will already be and the rules of the forum I should read, but why not ask here and there is not always time for everything. - AleksBak
  • Found like :) They are gray initially - these "green ticks". And noted where necessary (the person edited his answer several times, wrote later, since he wrote much / much more detailed ...) - AleksBak