Typedef
Encyclopedia : T : TY : TYP : Typedef
- The correct title of this } is }}}. The initial letter is capitalized due to [Naming conventions #Lower case first lettertechnical restrictions].
Consider this code:
#includevoid main()
Now consider this :
#includevoid main()
Both sections of code do the same thing: creates an int datatype (marks) and gives it a value of 100. The method of doing this in section two makes it easier to read because the typedefine statement causes student_mark_t to mean the same thing as int. In these examples, the variable marks stores the "marks" (or grades) of a student, so defining marks as a variable of type student_mark_t gives the variable name context.
One more example:
struct var ;Here a USER DEFINED datatype var has been defined. Thus, to create a variable of the type var, the following code is required:
struct var a;
Let's add the following line to the end of this example:
typedef struct var newtype;Now, in order to create a variable of type var, the following code will suffice:
newtype a;This is much easier to read because the keyword struct does not need to precede every variable of type var.
It is also possible to declare typedefs for arrays.
typedef BaseType NewType [arrSize]Doing this it is possible to declare a new array of type BaseType and size arrSize by writing:
NewType array;
External links
- [Cprogramming.com] - Detailed Discussion
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
