Definition of pointer type

Source: Internet
Author: User
Tags define function function definition int size

The syntax of a type definition boils down to a sentence: As long as you add a typedef to the variable definition, it becomes a type definition. What is supposed to be a variable here is a type.

int integer; Integral type variable
int *pointer; Integer pointer variable
int array [5]; Integral type array variable
int *p_array [5]; Variable of an array of integer pointers
int (*array_pointer) [5];//variable of a pointer to an integer array
int function (int param);//function definition, you can also treat function names as variables of functions
int *function (int param);//is still a function, but the return value is an integer pointer
Int (*function) (int param);//Now it's a pointer to a function.


To define the corresponding type, which is the name of the type, is the following form:
typedef int integer_t;                      //integral type
typedef int *pointer_t;     //integer pointer type
typedef int ARRAY_T [5];//integer array type
typedef int *P_ARRAY_T [5];   //int refers The type of the array of needles
typedef int (*array_pointer_t) [5];//The type of the pointer of an integer array
typedef int function_t (int param);    & nbsp function type
typedef int *function_t (int param);   //function type
typedef int (*function_t) (int param);//point to function The type of pointer
Note: The above function type may be faulted in C because there is no function type in C and its function variable is automatically degraded to function pointers; It seems to be possible in C + +. The main explanation here is the formal similarity.

The general form of a

typedef is:
typedef   type      definition name;
There are generally two uses of typedef in programming, One is to give the variable a new name that is easy to remember and meaningful, and the other is to simplify some of the more complex type declarations.
In fact, when declaring variables in C, there is a storage type indicator (Storage-class-specifier) that includes the familiar extern, static, auto, register. When you do not specify a storage type indicator, the compiler automatically takes the default values according to the conventions. In addition, the location of the storage type indicator is arbitrary (but required before the variable name and pointer *), which means that the following lines of code are equivalent:
static const int i;
const static int i;
int const static i;< br> const int static i;
According to the C language specification, the typedef and storage type indicators are equivalent when parsing is performed. So, we replace the above static with the typedef:
typedef const INT i;
const typedef int i;
int const typedef i;
Const int Ty Pedef i;
The semantics of the preceding code is to define I as a type name with the equivalent type of const int. If we have i   a code later, it is equivalent to const int A. The same is true for pointers, for example:
int const typedef *T; Then the code t   p. is equivalent to an int const *p.
In addition, a typedef cannot be used with storage type indicators such as static, because each variable can have only one storage type, so code: typedef static int i; is illegal.

There are two uses of a typedef:
One, general form, define an alias of an existing type
A typedef type definition name;
Second, create a new type
typedef return value type new type name (parameter list);

1) typedef int NUM[10];//Declare integer array type

NUM n;//defines n as an integer array variable, where n[0]--n[9] is available

2) typedef char* STRING;//Declare STRING as character pointer type

STRING p,s[10];//p is a character pointer variable and s is an array of pointers

3) typedef int (*pointer) ();//Declaration pointer is a pointer type that points to a function that returns an integer value with no parameters

Pointer p1,p2;//p1,p2 as a pointer variable of type pointer


1 use typedef can declare various types of names, but can not be used to define variables, with typedef can declare array type, string type, easy to use.

For example: Define an array, which used to be: int a[10],b[10],c[10],d[10]; because they are all one-dimensional arrays and the same size, you can declare this array type as a first name:

typedef int ARR[10];

Then use ARR to define the array variables:

ARR A,b,c,d;//arr is an array type that contains 10 elements. So a,b,c,d is defined as a one-dimensional array with 10 elements. As you can see, you can isolate array types and arrays by using a TypeDef, and you can define multiple array variables by using an array type. You can also define string types, pointer types, and so on.

2 use typedef to simply add a type name to an existing type without creating a new type.

3 the typedef and #define have similarities, but in fact the two are different, #define是在 precompiled processing, it can only do a simple string substitution, and the typedef is at compile-time processing. Rather than doing a simple string substitution, it declares a type as a method of defining a variable.

For example: typedef int COUNT, and #define COUNT int are all used to represent int, but in fact they are different.

Two traps:

Trap One:
Remember, a typedef is a new alias that defines a type, and unlike macro, it is not a simple string replacement. Like what:
First define:
typedef char* PSTR;
And then:
int mystrcmp (const PSTR, const PSTR);
is the const PSTR actually equivalent to the const char*? No, it's actually equivalent to char* Const.
The reason is that const gives the whole pointer itself a constant, that is, a constant pointer char* const.
In short, remember that when the const and the typedef appear together, the typedef is not a simple string replacement.
Trap Two:
A typedef is syntactically a storage class keyword (like auto, extern, mutable, static, register, etc.), although it does not really affect the storage characteristics of the object, such as:
typedef static INT INT2; Not feasible
Compilation will fail, prompting "more than one storage class specified."

How to use 1.typedef function pointers

(1) typedef is used first to define new types, i.e typedef struct {...} MyStruct; When referencing later, you can use MyStruct to define your own structure, mystruct structname1,mystruct structname2.

(2) A common place for TypeDef is to define function pointers, behave like macro definitions, replace synonyms with actual types, but there are differences: the typedef is interpreted at compile time, so the compiler is allowed to deal with text substitutions beyond preprocessor capabilities.

Case one:
Generally speaking, a typedef is better than a #define, especially in a pointer situation. Take a look at the example:
typedef char *PSTR1;
#define PSTR2 char *;
PSTR1 S1, S2;
PSTR2 S3, S4;
In the above variable definition, s1, S2, S3 are all defined as char *, and S4 is defined as char, not the pointer variable we expect, and the root cause is that #define is just a simple string replacement and the typedef is a new name for a type.
Case TWO:
The compiler in the following code will report an error, do you know which statement is wrong?
typedef char * PSTR;
Char string[4] = "ABC";
const char *P1 = string;
Const PSTR P2 = string;
It was p2++ that made a mistake. This question once again reminds us that typedef and #define are different, and that it is not a simple text replacement. The const PSTR P2 in the above code is not equal to the const char * p2. The const PSTR P2 and const long x are essentially indistinguishable, with read-only restrictions on the variable, except that the data type of the variable P2 here is our own definition rather than the intrinsic type of the system. Therefore, the meaning of the const PSTR P2 is that a variable that qualifies the data type char * is P2 read-only, and therefore p2++ error.

Usage One:

typedef return TYPE (* new type) (parameter table)

typedef INT (* myfunction) (int,int); This usage is generally in the definition function pointer myfunction is a function pointer type has two integral type parameter, returns an integral type.

In the case of this form, removing the TypeDef and aliases is left with the type of the original variable such as int (*) (int, int); In the function pointer, the abstract view function, the function name is actually an address, the function name points to the function's code at the first address of memory.

Usage Two: complex function declaration types

Here is a declaration of three variables how to do with a typedef ...

>1 int * (*a[5]) (void *,void *);

>2 void (*b[5]) (void (*) ());

>3 Float (*) (*PA) [10];

The analysis is as follows:

>1 int * (*a[5]) (void *,void *);

Pfun is its own type alias typedef int * (* pfun) (void *,void *); equivalent to int * (*a[5]) (void *,void *);

Pfun A[5]; A is an array that contains five elements, all of which are function pointers, and the function pointer refers to a function whose return value is a pointer input parameter of int and two are void *.

>2 void (*b[5]) (void (*) ());

A new type typedef void (*pfunparam) () is declared in the blue.

The whole declares a new type of typedef void (*pfun) (Funparam);

Declaring an object with a defined new type is equivalent to void (*b[5]) (void (*)) ();

Pfun B[5]; b is an array of 5 elements, each of which is a function pointer, and the return value of the function that the function pointer refers to is void. The input parameter is another function pointer that has no arguments and the return value is null. A continuous function pointer is applied here. itself is a function pointer, and the parameter is also a function pointer.

>3 Float (*) (*PA) [10];

The blue expression above declares a new type of typedef float (*pfun) ();

The whole declares a new type of typedef pfun (* pfunparam) [10];

Declares an object equivalent to float (*) () (*PA) [10] using a defined new type;

PA is a pointer to an array of 10 elements, the element of the array is a function pointer, the function pointer does not have an input parameter, and the return value is float.


Use typedef to simplify complex variable declarations
1), define an array of 10 pointers to a function that has an integer parameter and returns an integral type.
The first method: Int (*a[10]) (int);
The second method: TypeDef int (*PFUNC) (int);
             Pfunc a[10];
2), which defines an array of 10 pointers to a function that has a function pointer (without arguments, the return value is null), and returns NULL.
The first method: void (*a[10]) (void (*) (void));
The second method: typedef void (*pfuncparam) (void);
               typedef void (*PFUNC) ( Pfuncparam);
Pfunc a[10];
3), a pointer to an array of 10 function pointers (without arguments, with a double)
First method: Double (*) (void) (*P) [ten];
Second method: typedef double (*PFUNC) ( void);
             typedef pfunc (*pfuncparam) [10];
             Pfuncparam p;

From the variable name look, first to the right, and then to the left, a round of parentheses to turn the reading direction; After parsing in parentheses, jump out of parentheses, or in the order of the first right and left, so cycle until the entire statement is analyzed. Example:
Int (*func) (int *p);
First find the variable name func, which has a pair of parentheses outside, and a * number on the left, which means that func is a pointer, and then jumps out of the parentheses, looks to the right, and then encounters parentheses, which means that (*FUNC) is a function,

So func is a pointer to this type of function, the function pointer, which has a int* type parameter, and the return value type is int.
Int (*func[5]) (int *);
Func to the right is an [] operator, which shows that Func is an array of 5 elements; func has a * on the left, which means that the elements of Func are pointers (note that the * is not modifying func, but modifying func[5].

The reason is that [] operator precedence is higher than *, Func is first combined with []. Jump out of this bracket, look to the right, and encounter parentheses, stating that the element of the Func array is a pointer to a function type, which points to a function with a int* type parameter.

The return value type is int.
You can also remember 2 modes:
Type (*) (...) function pointers
Type (*) [] array pointer



The most common use of a typedef is to create a type name that is easy to remember, and use it to archive the programmer's intent. The type is now declared in the variable name, located on the right side of the ' typedef ' keyword. For example:
typedef int size; This declaration defines a synonym for int, with a name of size. Note that a typedef does not create a new type. It simply adds a synonym to an existing type. You can use size in any context that requires int:
void measure (Size * psz); Size Array[4];size len = File.getlength (); std::vector <size> vs; A typedef can also mask conforming types, such as pointers and arrays. For example, you don't have to repeatedly define an array of 81 character elements as follows:
Char Line[81];char text[81]; Defines a typedef that you can do whenever you want to use an array of the same type and size:
typedef char LINE[81]; Line text, secondline;getline (text); Similarly, you can hide the pointer syntax as follows:
typedef char * Pstr;int mystrcmp (pstr, pstr); This will take us to the first typedef trap. The standard function strcmp () has two parameters for the ' const char * ' type. Therefore, it may mislead people to declare mystrcmp () as follows:
int mystrcmp (const PSTR, const PSTR); This is wrong, in order, ' const PSTR ' is interpreted as ' char * const ' (a constant pointer to char), not ' const char * ' (a pointer to a constant char). This problem is easy to solve:
typedef const CHAR * CPSTR; int mystrcmp (CPSTR, CPSTR); Now is the right thing to remember: whenever you declare a typedef for a pointer, you add a const to the final typedef name so that the pointer itself is a constant, not an object.
Code simplification
The typedef behavior discussed above is somewhat like #define macro, replacing synonyms with its actual type. The difference is that the TypeDef is interpreted at compile time, so the compiler is allowed to deal with text substitutions beyond preprocessor capabilities. For example:
typedef int (*PF) (const char *, const char *); This declaration introduces the PF type as a synonym for function pointers, which has two parameters of the const char * type and a return value of type int. This typedef is essential if you want to use the following form of a function declaration:
PF Register (pf pf); The parameter of the Register () is a PF-type callback function that returns the address of a function with the same signature as the previously registered name. Take a deep breath. Let me show you how to implement this declaration without a typedef:
Int (*register (*PF) (const char *, const char *)) (const char *, const char *); Few programmers understand what it means, let alone the risk of error caused by this convoluted code. Obviously, using typedef here is not a privilege, but a necessity. Skeptics may ask, "OK, does anyone still write this code?" "A quick glance at the header file <csinal&gt that reveals the signal () function, a function with the same interface.
typedef and Storage class keywords (storage class specifier)
This is not a bit surprising, the TypeDef, like Auto,extern,mutable,static, is a storage-class keyword, as is the register. This is to say that a typedef really affects the storage characteristics of an object; it simply says that the TypeDef declaration looks like a variable declaration of a type such as Static,extern on the composition of the statement. Here's a second trap:
typedef register INT Fast_counter; Incorrect compilation pass. The problem is that you can't have multiple storage-class keywords in the declaration. Because the symbol typedef already occupies the location of the storage class keyword, the register (or any other storage class keyword) cannot be used in a typedef declaration.
Promotion of Cross-platform development
There is another important use of TypeDef, which is to define machine-independent types, for example, you can define a floating-point type called real, and on the target machine it can achieve the highest precision:
typedef long double Real; On a machine that does not support long double, the typedef looks like the following:
typedef double REAL; And, on a machine that does not have a double, the typedef looks like this:
typedef float REAL; Instead of making any changes to the source code, you can compile the application with real type on each platform. The only thing to change is the typedef itself. In most cases, even this tiny change can be done automatically through wonderful conditional compilation. Isn't it? The standard library uses typedef extensively to create such platform-independent types: Size_t,ptrdiff and fpos_t are examples. In addition, a typedef like std::string and Std::ofstream also hides a long, incomprehensible template-specific syntax, such as: Basic_string<char, Char_traits<char> Allocator<char>> and Basic_ofstream<char, char_traits<char>>.

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: 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.