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.
typedefdemand 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