Differences between typeof and tpyedef

Source: Internet
Author: User

Transferred from:

Http://blog.csdn.net/gexueyuan/article/details/6930228

Understanding 1:

Typedef is used to define the type alias, which is available in C/C ++ and is a feature of language and has nothing to do with MFC.

For example
Typedef int * intptr;
Intptr A; // equivalent to int *;

Typeof, I know an extension of the C/C ++ syntax in GCC, used to obtain the parameter type statically
For example
Int A = 3;
Typeof (a) B = 4; // equivalent to int B = 4;
Typeof ("12345") C = "ABCDE"; // equivalent to const char C [6] = "ABCDE"

Vector <typeof (1.234)> A; // equivalent to vector <double>;

Understanding 2:

From: http://blog.csdn.net/wslong/article/details/7728811

The typeof keyword is a new extension in C.

The typeof parameter can be in two forms:ExpressionOrType.
The following is an example using an expression:
Typeof (X [0] (1)
Assume that X is a function pointer array to obtain the type of the function return value.
If typeof is used as an expression, the expression is not executed. Only the type of the expression is obtained.
The following example declares the VaR variable of the int type because the expression Foo () is of the int type. Because the expression is not executed, the foo function is not called.
Extern int Foo ();
Typeof (FOO () var;

The following is an example of parameter type:
Typeof (int *) A, B;
It is equivalent:
Int * a, * B;

The following two equivalent declarations are used to declare the int type variable.
Typeof (INT) A;/* int type */
Typeof ('B') A;/* the type of this expression in GCC is int (automatically upgraded to int ),
Note that typeof (char) and typeof ('B') are not the same. This can be seen using sizeof */

Generally, you can use typeof, but if you want to be compatible with iso c, it is best to use the double-underline format: __typeof __.
Typeof is similar to typedef. In fact, typeof can be used wherever typedef is used.

Here are some other examples:
Define y as the data type pointed to by X:
Typeof (* x) y;
Define y to point X to an array of data types:
Typeof (* x) y [4];
Define y as a character pointer array:
Typeof (char *) [4] Y;
This is equivalent to the following definition:
Char * Y [4];

Let's change the definition method:
# Define pointer (t) typeof (T *)
# Define array (t, n) typeof (T [N])
Array (pointer (char), 4) y;

If we want to define T as the type of an expression, we can only use typedef.
However, you can use typeof:
Typdef typeof (expr) T;

Declaration example using typeof
BelowExampleDeclares pointers and arrays. For comparison, an equivalent declaration without typeof is provided.
Typeof (int *) P1, P2;/* declares two int pointers p1, p2 */
Int * P1, * P2;

Typeof (INT) * P3, P4;/* declares int pointer P3 and INT P4 */
Int * P3, P4;

Typeof (INT [10]) a1, a2;/* declares two arrays of integers */

Int A1 [10], A2 [10];

Restrictions on the Use of typeof statements
Note that the type name in the typeof construction cannot contain the storage class specifier, such as extern or static. However, it can contain type delimiters, such as const or volatile.
For example, the following code is invalid because it declares extern In the typeof construction:
Typeof (extern INT);

The following code uses an external link to declare that identifier B is valid, indicating an int type object. The next statement is also valid. It declares a char type pointer using the const qualifier, indicating that the pointer P cannot be modified.
Extern typeof (INT) B;
Typeof (char * const) P = "";

Use typeof in macro Declaration
Typeof is mainly used in macro definition. You can use the typeof keyword to reference the type of macro parameters. Therefore, it is possible to construct an object with the required type without explicitly specifying the type name as a macro real parameter.
The following is a macro definition for exchanging values of two variables:
# Define swap (a, B ){\
Typeof (a) _ t = ;\
A = B ;\
B = _ t ;}
This macro can swap all the variables of the basic data type (integer, character, structure, etc)

Refer:
Http://blog.chinaunix.net/u3/101356/showart_2081601.html
Http://gcc.gnu.org/onlinedocs/gcc/Typeof.html#Typeof

The typeof keyword is a new extension in C.

The typeof parameter can be in two forms:ExpressionOrType.
The following is an example using an expression:
Typeof (X [0] (1)
Assume that X is a function pointer array to obtain the type of the function return value.
If typeof is used as an expression, the expression is not executed. Only the type of the expression is obtained.
The following example declares the VaR variable of the int type because the expression Foo () is of the int type. Because the expression is not executed, the foo function is not called.
Extern int Foo ();
Typeof (FOO () var;

The following is an example of parameter type:
Typeof (int *) A, B;
It is equivalent:
Int * a, * B;

The following two equivalent declarations are used to declare the int type variable.
Typeof (INT) A;/* int type */
Typeof ('B') A;/* the type of this expression in GCC is int (automatically upgraded to int ),
Note that typeof (char) and typeof ('B') are not the same. This can be seen using sizeof */

Generally, you can use typeof, but if you want to be compatible with iso c, it is best to use the double-underline format: __typeof __.
Typeof is similar to typedef. In fact, typeof can be used wherever typedef is used.

Here are some other examples:
Define y as the data type pointed to by X:
Typeof (* x) y;
Define y to point X to an array of data types:
Typeof (* x) y [4];
Define y as a character pointer array:
Typeof (char *) [4] Y;
This is equivalent to the following definition:
Char * Y [4];

Let's change the definition method:
# Define pointer (t) typeof (T *)
# Define array (t, n) typeof (T [N])
Array (pointer (char), 4) y;

If we want to define T as the type of an expression, we can only use typedef.
However, you can use typeof:
Typdef typeof (expr) T;

Declaration example using typeof
BelowExampleDeclares pointers and arrays. For comparison, an equivalent declaration without typeof is provided.
Typeof (int *) P1, P2;/* declares two int pointers p1, p2 */
Int * P1, * P2;

Typeof (INT) * P3, P4;/* declares int pointer P3 and INT P4 */
Int * P3, P4;

Typeof (INT [10]) a1, a2;/* declares two arrays of integers */

Int A1 [10], A2 [10];

Restrictions on the Use of typeof statements
Note that the type name in the typeof construction cannot contain the storage class specifier, such as extern or static. However, it can contain type delimiters, such as const or volatile.
For example, the following code is invalid because it declares extern In the typeof construction:
Typeof (extern INT);

The following code uses an external link to declare that identifier B is valid, indicating an int type object. The next statement is also valid. It declares a char type pointer using the const qualifier, indicating that the pointer P cannot be modified.
Extern typeof (INT) B;
Typeof (char * const) P = "";

Use typeof in macro Declaration
Typeof is mainly used in macro definition. You can use the typeof keyword to reference the type of macro parameters. Therefore, it is possible to construct an object with the required type without explicitly specifying the type name as a macro real parameter.
The following is a macro definition for exchanging values of two variables:
# Define swap (a, B ){\
Typeof (a) _ t = ;\
A = B ;\
B = _ t ;}
This macro can swap all the variables of the basic data type (integer, character, structure, etc)

Refer:
Http://blog.chinaunix.net/u3/101356/showart_2081601.html
Http://gcc.gnu.org/onlinedocs/gcc/Typeof.html#Typeof

The typeof keyword is a new extension in C.

The typeof parameter can be in two forms:ExpressionOrType.
The following is an example using an expression:
Typeof (X [0] (1)
Assume that X is a function pointer array to obtain the type of the function return value.
If typeof is used as an expression, the expression is not executed. Only the type of the expression is obtained.
The following example declares the VaR variable of the int type because the expression Foo () is of the int type. Because the expression is not executed, the foo function is not called.
Extern int Foo ();
Typeof (FOO () var;

The following is an example of parameter type:
Typeof (int *) A, B;
It is equivalent:
Int * a, * B;

The following two equivalent declarations are used to declare the int type variable.
Typeof (INT) A;/* int type */
Typeof ('B') A;/* the type of this expression in GCC is int (automatically upgraded to int ),
Note that typeof (char) and typeof ('B') are not the same. This can be seen using sizeof */

Generally, you can use typeof, but if you want to be compatible with iso c, it is best to use the double-underline format: __typeof __.
Typeof is similar to typedef. In fact, typeof can be used wherever typedef is used.

Here are some other examples:
Define y as the data type pointed to by X:
Typeof (* x) y;
Define y to point X to an array of data types:
Typeof (* x) y [4];
Define y as a character pointer array:
Typeof (char *) [4] Y;
This is equivalent to the following definition:
Char * Y [4];

Let's change the definition method:
# Define pointer (t) typeof (T *)
# Define array (t, n) typeof (T [N])
Array (pointer (char), 4) y;

If we want to define T as the type of an expression, we can only use typedef.
However, you can use typeof:
Typdef typeof (expr) T;

Declaration example using typeof
BelowExampleDeclares pointers and arrays. For comparison, an equivalent declaration without typeof is provided.
Typeof (int *) P1, P2;/* declares two int pointers p1, p2 */
Int * P1, * P2;

Typeof (INT) * P3, P4;/* declares int pointer P3 and INT P4 */
Int * P3, P4;

Typeof (INT [10]) a1, a2;/* declares two arrays of integers */

Int A1 [10], A2 [10];

Restrictions on the Use of typeof statements
Note that the type name in the typeof construction cannot contain the storage class specifier, such as extern or static. However, it can contain type delimiters, such as const or volatile.
For example, the following code is invalid because it declares extern In the typeof construction:
Typeof (extern INT);

The following code uses an external link to declare that identifier B is valid, indicating an int type object. The next statement is also valid. It declares a char type pointer using the const qualifier, indicating that the pointer P cannot be modified.
Extern typeof (INT) B;
Typeof (char * const) P = "";

Use typeof in macro Declaration
Typeof is mainly used in macro definition. You can use the typeof keyword to reference the type of macro parameters. Therefore, it is possible to construct an object with the required type without explicitly specifying the type name as a macro real parameter.
The following is a macro definition for exchanging values of two variables:
# Define swap (a, B ){\
Typeof (a) _ t = ;\
A = B ;\
B = _ t ;}
This macro can swap all the variables of the basic data type (integer, character, structure, etc)

Refer:
Http://blog.chinaunix.net/u3/101356/showart_2081601.html
Http://gcc.gnu.org/onlinedocs/gcc/Typeof.html#Typeof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.