Reserved naming in C / C ++ typedefs / structs

#include <stdio.h> #include <string.h> const int NAMELEN=30; const int MAXCLASSSIZE=10; typedef struct StudentRec { char lastname[NAMELEN]; char firstname[NAMELEN]; long int ID; int finalmark; }Student; 

I am new to coding ... and I have a question why there is a Student; after brackets .. this is the format we must follow.

+6
c ++ c
source share
5 answers

you mix two things. In C, you can define a structure like this:

 struct foo { int a, b; }; 

Then, to use this, you must do this:

 struct foo myFoo; 

This is really verbosity, so they had this brilliant idea of ​​using typedef to create a new type. I can do something like this:

 typedef int myInt; 

and then use it:

 myInt x; 

So what you do is declare that there is a new type, Student, which is equivalent to the StudentRec structure. By convention, many people use the same name for typedef as for structure - this is legal:

 typedef struct foo { int a, b; } foo; 
+17
source share

This creates a student type name to indicate the structure. Using typedef for this purpose is not required in C ++. It can be defined as follows:

 struct Student { char lastname[NAMELEN]; char firstname[NAMELEN]; long int ID; int finalmark; }; 
+13
source share

In C, there are different namespaces for structure names and type names (for example, int) {the type namespace is shared with the variable and function namespace, so keep that in mind}. When you define a new structure, you automatically add its name to the struct namespace, but when you declare a variable of this type, you must prefix it with the name with struct so that the compiler knows to look in the struct namespace to find out what it is what you want this variable to be.

Using the typedef keyword, you can add a name for a variable to the type namespace so you don't have to use the struct keyword in your declarations.

The example you provided combined a typedef declaration with a structure definition, but used different names for the structure in the type namespace and the struct namespace. You can do this for two reasons. First, an instruction prefix that looks exactly like declaring a variable with typedef defines the type name instead of the variable with the specified name. Secondly, you can declare structure type variables by inserting their names immediately after the structure and semicolon are declared. Your example combined them.

There are other legal forms in C. For example:

 /* This declares a variable foo whose type has no name but is a struct that contains one int named x */ struct { int x; } foo; /* This adds bar to the type namespace without adding an entry to the struct namespace. */ typedef struct { int y; } bar; /* This adds the name baz to the struct namespace and declares a variable named b of this type */ struct baz { int z; } b; /* This adds the name ralf to the type namespace which also refers to this type */ typedef struct baz ralf; 

C ++ has a different namespace structure, which I'm sure you noticed, as it has the namespace keyword. In this case, although C ++ is simpler than C. Defining a structure (or class or union or enumeration) automatically adds the name you used (if any) to the namespace in which typedef adds the names. This greatly simplifies, for the most part, but there are a few errors. This is mainly due to maintaining backward compatibility with C. In most cases, this compatibility is related to the remark when someone typedefs struct foo foo; and does not see it as a mistake when trying to name something with an already used name.

Another problem is related to the fairly common C code:

 struct shoe { int size; } shoe; 

This is common in C, especially when only one of the structures should exist. In C, this would be easy because there would be no collision between the names of struct shoe and the variable shoe . In C ++, this still works, although to maintain backward compatibility with C.

+4
source share

Student is the name of a new type created by typedef. StudentRec is the name of the structure that it defines.

0
source share

Nothing applies to C++ . In C++ preferable:

 struct Foo { . . . }; 

The rest applies only to C

Technically, struct Foo {}; enough for most purposes. However, this is a bit verbose, as struct must be repeated every time the Foo type is used.

 struct Foo {} void func( struct Foo* foo ); 

typedef makes this easier.

 struct Foo { }; typedef struct Foo Foo; void func( Foo* foo ); 

This can be further reduced:

 typedef struct Foo { } Foo; void func( Foo* foo ); 

When executing a single liner, it can also use this syntax:

 typedef struct { } Foo; void func( Foo* foo ); 

This creates an anonymous struct and then gives it the name Foo. You most often see this with enum s.

 typedef enum { } Bar; 

There is a reason that excess Foo usually remains. This is the only way to create a self-reference structure, such as a linked list.

 typedef struct Node { Node* next; } Node; 

If the starting Node , where ommited, there would be no way to declare a pointer to a structure. Technically, this is true, but now you need to come up with 2 names, not just one.

 typedef struct node_ { node_* next; } Node; 

So why use:

 typedef struct Foo { } Foo; 

For homogeneity. This declaration style covers all your bases, so you don’t need to think about it when declaring a structure.

0
source share

All Articles