Go: typeof keyword in C language

Source: Internet
Author: User

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

The TypeOf keyword is a new extension in the C language.

The arguments to TypeOf can be in two forms: an expression or a type .
Here is an example of using an expression:
typeof (X[0] (1)
This assumes that X is an array of function pointers, so that you can get the type of the function return value.
If typeof is used in an expression, the expression is not executed. Only the type of the expression is given.
The following example declares a var variable of type int, because the expression foo () is of type int. The Foo function is not called because the expression is not executed.
extern int foo ();
typeof (Foo ()) var;

The following is an example of using a type as a parameter:
typeof (int *) A, B;
Equivalent to:
int *a,*b;

Here are two equivalent declarations for declaring variable a of type int.
typeof (int) A; /*int Type */
typeof (' B ') A; /* The type of this expression in gcc is int (automatically promoted to int),
Note that typeof (Char) and typeof (' B ') do not get the same, and this can be seen with sizeof */

In general, it is possible to use TypeOf, but if you want to be compatible with ISO C, it is best to use a double-underline form: __typeof__.
typeof and TypeDef are very similar, in fact, as long as can use a TypeDef place can use TypeOf.

Here are some other examples:
Define y as the data type that x points to:
typeof (*x) y;
Define y as an array of x points to the data type:
typeof (*x) y[4];
Define y as an array of character pointers:
typeof (typeof (char *) [4] y;
This is equivalent to the following definition:
Char *y[4];

Let's change the definition in another way:
#define Pointer (t) typeof (T *)
#define Array (t,n) typeof (T [N])
Array (pointer (char), 4) y;

If you want to define t as the type of an expression, we just can't do it with a typedef.
But you can do it through typeof:
Typdef typeof (expr) T;

examples of declarations using typeof
FollowingExampleUsed to declare pointers and arrays. In order to compare, the equivalent declaration without typeof is also given.
typeof (int *) P1,P2; /* declares-int pointers P1, p2 */
int *P1, *P2;

typeof (int) *p3,p4;/* declares int pointer p3 and int P4 */
int *P3, P4;

typeof (int []) A1, A2;/* declares, arrays of integers * *

int a1[10], a2[10];

declaring restrictions using typeof
Note that the type name in the TypeOf construct cannot contain a storage-class descriptor, such as extern or static. However, the inclusion of a type qualifier, such as const or volatile, is allowed.
For example, the following code is invalid because it declares extern in the TypeOf construct:
typeof (extern int) A;

The following code uses an external link to declare that identifier B is valid and represents an object of type int. The next declaration is also valid, which declares a char type pointer that uses the Const qualifier, indicating that the pointer p cannot be modified.
extern typeof (int) b;
typeof (char * const) p = "a";

using typeof in a macro declaration
The main application of the TypeOf construct is in the macro definition. You can use the TypeOf keyword to refer to the type of the macro parameter. Therefore, it is possible to construct an object with the desired type without explicitly specifying the type name as a macro argument.
The following is a macro definition that swaps the values of two variables:
#define SWAP (A, b) {\
typeof (a) _t=a;\
A=b;\
b=_t;}
This macro can exchange variables of all basic data types (integers, characters, structures, etc.)

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

Go: typeof keyword in C language

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.