C language NOTE 3 (zz)

Source: Internet
Author: User
Tags call by reference
2.3 pointer type
The pointer variable stores the memory address. If the address stores the variable, the pointer is called the pointer to the variable. If the address corresponds to the entry of a function, the pointer is called a pointer to a function.

2.3.1 pointer to a variable
Add the "*" sign before the variable name to define the pointer. Note: "Before the variable name" is mentioned here, instead of "after the type description", because the latter method may cause misunderstanding. The next approach seems to be logical: add the "*" sign after the type description to construct a new data type-pointer type pointing to this type.
However, if the following definition appears in the program:
Arcnode * p, q;
So, Q is a pointer? Think back to the definition of the array, if the program shows:
Int zlen [dct_size * dct_size], CNT;
No one will think that CNT is also an array. "*" And "[]" are the variables that are close to them. So why do we intentionally misunderstand "*" after the type description? In a compromise, when the definition of a pointer is involved, a variable is defined in one row, so the above definition becomes:
Arcnode * P;
Arcnode Q;

There are two confusing concepts: pointer array and pointer to array:
Int * PTR;
PTR is a pointer variable pointing to an integer type. After the variable name is added, "[]" is changed:
Int * (PTR [max_length]); or int * PTR [max_length];
In this case, PTR becomes an array with the length of max_length pointer. Its element is a pointer variable pointing to an integer type.
Similarly:
Int Val [max_length];
Val is an integer array. Add "*" before and after the variable name:
INT (* val) [max_length];
Because the default operator combines priority, parentheses must be added to emphasize the order of combination.
In this case, Val is a pointer to an integer array.
This is a simple aggregation of the original variable type with '[]' After the variable name, into an array, and '*' before the variable name, into a pointer, pointing to the original variable type ." .

2.3.2 function pointer
Function pointers are tools used to parameterize functions. Many C-database functions use function pointers as parameters, such as bsearch and qsort:
Qsort (void *) List, length, sizeof (INT), comp );
The function name comp identifies the function entry address and uses it as an actual parameter. It is passed to the function pointer form parameter of qsort. The parameter is defined:
INT (* fcmp) (const void *, const void *)
Compare the definition of the comp function:
Int comp (const void * P1, const void * P2 ){
...
}
Qsort uses the fcmp parameter to call the comp function to sort the elements in the array.

Function pointers can also be used to control code bodies. In addition, you can create a table-driven application by using the array of function pointers.

2.3.3 pointer Application

(1) construct a chain data structure. For details, see the application of the structure (3)

(2) reference the dynamically allocated data structure, such as the Linear Linked List structure:
Void createlist_l (linklist * l, int N ){
Linklist P;
Int I;
* L = (linklist) malloc (sizeof (lnode ));
(* L)-> next = NULL;
For (I = N; I> 0; -- I ){
P = (linklist) malloc (sizeof (lnode ));
Scanf ("% d", & P-> data );
P-> next = (* l)-> next;
(* L)-> next = P;
}
}

(3) call by reference. For details, see application of struct (2)

(4) access and iterate data elements, such:
Status getelem_l (linklist L, int I, int * E ){
Int J = 1;
Linklist P = L-> next;
While (P! = NULL & J <I ){
P = p-> next; ++ J;
}
If (P = NULL | j> I) return error;
* E = p-> data;
Return OK;
}

(5) pass array parameters, such:
Void makehufftable (DWORD code [], byte size [], const byte bit [], const byte Val []) {
...
}
Code, size, bit, and Val are all defined in the form of arrays to receive array arguments, but they are actually pointer variables.

(6) reference a function. For example, in binary tree traversal, access the function call:
Status preordertraverse (bitree T, status (* visit) (char )){
If (T! = NULL ){
If (visit (t-> data ))
If (preordertraverse (t-> lchild, visit ))
If (preordertraverse (t-> rchild, visit) Return OK;
Return Error;
}
Else Return OK;
}

(7) alias for other values

(8) represents a string, such:
Lpctstr lpszmenuname = "testbmp menu ";

(9) directly access the system memory. The following is the code snippet for accessing the Display memory in the DOS environment:
# Define mk_fp (SEG, OFS) (void far *) (unsigned long) (SEG) <16) | (unsigned) (OFS )))
...
Byte * P = mk_fp (0xa000, offset );
...
If (t_bpp = 1)
* P = g;
Else
Memcpy (p, & t_color [g], t_bpp );
The dos storage space layout is:
Segment address length usage
0000 h 640 K basic memory
A000h 128 K ing Display memory
C000h 256 k BIOS Rom Zone
Macro mk_fp is used to construct a remote pointer (32-bit, segment address <16 + segment offset) based on the given segment address (16-bit) and intra-segment offset (16-bit ). The physical address value of the actual storage location is 20 bits (segment address <4 + intra-segment offset ). 8086/8088 has only 20 address lines, so the maximum addressing space is 1 m.
In this case, the memory area pointed to by the remote pointer P is not defined as a variable, and it directly accesses the system memory.

2.4 function data type
The data type of the function, that is, the Data Type of the function return value, can be any type other than the array type in principle. Of course, the function can also have no return value. In this case, the data type of the function is void.

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.