Selasa, 03 Juli 2018

Sponsored Links

C Programming Tutorial - 72: Typedef - YouTube
src: i.ytimg.com

typedef is the backup keyword in C and C programming languages. This is used to create an alias name for another data type. Thus, it is often used to simplify the syntax of declaring complex data structures composed of struct and union types, but it is equally common to provide names of specific descriptive types for integer data types of varying lengths. Standard C library and POSIX backup suffix '_t', for example as in size_t and time_t.


Video Typedef



Syntax

The syntax of the typedef declaration is:

typedef type declaration ;

Maps Typedef



Example

 typedef int length;  

This creates the type length as a synonym of type int .

Indicates the meaning of the variable definition

Typedef declarations can be used as documentation by showing the meaning of variables in the context of programming, for example, it may include expression unit measurement or number. Generic declarations,

can be expressed by declaring a specific type of context:

Both sections of the code run identically. However, the use of typedef declarations in the second code block makes it clear that two variables, while represented by the same data type int , represent different or incompatible data. The definition in congratulate () your_score shows the programmer that current_speed (or another variable not declared as point ) is not may be passed as an argument. It will not be visible if both are expressed as variables of the int datatype. However, the indication is for programmer only ; the C/C compiler considers both variables as type int and does not indicate a warning or error type mismatch for the "wrong" argument type to congratulate (pointing your_score) in the code snippet at below:

Although the compiler considers km_per_hour equals int , both can not be used interchangeably when the type is changed by the unsigned , or long .

Simplifying definitions or statements

Typedef can be used to simplify compound type declarations (struct, union) or pointer types. As an example,

Here, the data type struct MyStruct is defined. To declare variables of this type in C, the struct keyword is required (although it may be omitted in C):

Typedef can be used to eliminate the need for the keyword struct in C. For example, a declaration

changed into:

The structure and typedef declarations can instead be merged into one statement:

Or maybe used as follows:

In C, unlike C, the struct , class , and enum keywords are optional in the variable declarations that are separate from the definition, as long as there is no ambiguity to other identifiers:

Thus, MyStruct can be used anywhere newtype can be used. However, the opposite is incorrect; for example, the constructor method for MyStruct can not be named newtype .

A well-known example where even C requires the keyword struct is a POSIX stat system call that uses struct of the same name in its argument:

Here both C and C require the struct keyword in the parameter definition.

Using typedef with pointer

We can use a typedef to define a new pointer type. As an example,

Above, intptr is a new alias with type pointer int * . Definition, intptr ptr; , defines the variable ptr with type int * . So ptr is a pointer that can point to a memory with type int .

Using typedef to specify a new pointer type can sometimes cause confusion. As an example:

Above, intptr cliff, allen; means defining 2 variables with type int * for both. This is because the type defined by typedef is of type, not expansion. In other words, intptr , which is type int * , adorns the abyss and allen. For intptr cliff2, * allen2; , type intptr adorns cliff2 and * allen2 . So, intptr cliff2, * allen2; is equivalent to 2 separate definitions, intptr cliff2; and intptr * allen2 . intptr * allen2 means that allen2 is a pointer pointing to memory of type int * . In short, allen2 has a type, int ** .

Using typedef with the structure pointer

Typedefs can also simplify definitions or declarations for the type of structure pointer. Consider this:

Using typedef, the above code can be rewritten like this:

In C, one can declare multiple variables of the same type in one statement, even mixing structures with a pointer or non-pointer. However, one needs an asterisk prefix for each variable to designate it as a pointer. Here, a programmer might assume that errptr is indeed a Node * , but typographical errors mean that errptr is Node This can lead to subtle syntactical errors.

By defining typedef Node * , it is certain that all variables are structural pointer types, or say, that each variable is a pointer type pointing to the type of structure.

Using typedef with function pointer

Consider the following code, which does not use typedef:

This code can be rewritten with typedef as follows:

Here, MathFunc is the new alias for that type. A MathFunc is a pointer to a function that returns an integer and retrieves as a floating argument followed by an integer.

When a function returns a function pointer, it can be more confusing without typedef. The following is a function prototype of signal (3) from FreeBSD:

The above function declarations are vague because they do not clearly indicate what functions are accepted as arguments, or types that are returned. A novice programmer can even assume that the function accepts one int as its argument and produces nothing, but in reality it also needs a function pointer and returns the function of another pointer. Can be written more neatly:

Using typedef with array

Typedef can also be used to simplify the definition of array types. As an example,

Here, arrType is a new alias for type "char [6]", which is an array type with 6 elements. For arrType * pArr; , pArr is a pointer that points to the type "char [6]" memory.

1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last ...
src: images.slideplayer.com


Using typedef with cast type

Typedef is created using the definition syntax but can be used as if it was created using the cor syntax. (Type casting changes the data type.) For example, in each row after the first row:

funcptr is used on the left side to declare variables and use on the right side to assign values. Thus, typedefs can be used by programmers who do not want to figure out how to convert the definition syntax to typing the cast syntax.

Note that, without typedefs, it is generally not possible to use syntax definitions and syntax interchangeably. As an example:

C-C++ Typedefs - YouTube
src: i.ytimg.com


Usage worries

Kernigan and Ritchie state in their book The C Programming Language two reasons for using typedefs. First, it provides the means to make the program more portable or more manageable. Instead of having to change the ubiquitous type it appears in the entire source file of the program, only a single typedef statement needs to be changed. Second, typedefs can make complex definitions or declarations easier to grasp.

Some people oppose the extensive use of typedefs. Most arguments center on the notion that typedefs only hide the actual data type of a variable. For example, Greg Kroah-Hartman, the Linux kernel and documentary hacker, does not advocate its use for anything but the function prototype declaration. He argues that this practice not only does not need to obscure code, it can also cause programmers to inadvertently abuse large structures that regard them as simple types.

1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last ...
src: images.slideplayer.com


Usage in C

In the names of type C can be very complicated and typedef provides a mechanism for assigning a simple name to the type. Consider:

and

Use with template

C 03 does not provide templated typedefs. For example, to have a stringpair & lt; T & gt; represents the std :: pair & lt; std :: string, T & gt; for each type T one can not use:

However, if someone wants to receive stringpair & lt; T & gt; :: type instead of stringpair & lt; T & gt; it is possible to achieve the desired result via typedef in an otherwise unused class or struct :

In C 11, templated typedefs are added with the following syntax, which requires using keywords instead of typedef keywords. (See template alias.)

typedef C++ - YouTube
src: i.ytimg.com


Other languages ​​

In many statically typed functional languages, such as Haskell, Miranda, OCaml, etc., one can define synonym type , which is similar to typedefs in C. Example in Haskell:

This example has defined the synonym type PairOfInts as an integer type.

In Seed7 the definition of a constant type is used to introduce a synonym for a type:

Di Swift, typedef disebut typealias :

C # contains features similar to typedef or syntax using from C.

In D keyword alias makes it possible to create a type or part of a synonym type.

01.28 - C Basics - Structs and typedef example - YouTube
src: i.ytimg.com


See also

  • Type of abstract data
  • C syntax

How to union struct of struct with marshal.sizeof c#/c++ - Stack ...
src: i.stack.imgur.com


References

Source of the article : Wikipedia

Comments
0 Comments