Indispensable Windows Native (7), windowsnative
[Download source code]
Indispensable Windows Native (7)-C language: pointer
Author: webabcd
Introduction
Indispensable C language for Windows Native
Example
CPointer. h
#ifndef _MYHEAD_POINTER_#define _MYHEAD_POINTER_ #ifdef __cplusplus extern "C"#endif char *demo_cPointer();#endif
CPointer. c
/** Pointer */# include "pch. h "# include" cPointer. h "# include" cHelper. h "void Merge (); void pointer_demo2 (); void pointer_demo3 (); void pointer_demo4 (); void Merge (); void pointer_demo6 (); void pointer_demo7 (); void evaluate (); void pointer_demo9 (); void pointer_demo10 (); char * demo_cPointer () {// basic concepts of pointer pointer_demo1 (); // string pointer pointer_demo2 (); // pass the pointer as a parameter to pointer_demo3 (); // array pointer Operation point Er_demo4 (); // pointer_demo5 (); // function pointer (pointer to function) pointer_demo6 (); // pointer-type function (the return value of the function can be a pointer) pointer_demo7 (); // pointer array pointer_demo8 (); // pointer pointer_demo9 (); // void pointer pointer_demo10 (); return "view code and comment";} // basic concept of pointer void pointer_demo1 () {/** note: * 1. You cannot use an unassigned pointer variable. * 2. A pointer variable can only point to variables of the same type. For example, if pointer p points to an integer variable, you can no longer point to character variables or other non-integer variables * // define a pointer and assign it to a NULL pointer (corresponding macro definition # define NULL 0 ). The so-called NULL pointer is a pointer that does not point to any content. int * p_empty = NULL; // equivalent to int * p = 0; // defines an integer variable and initializes int I = 100; // * p-Indicates defining a pointer named p. The "*" here is the type specifier, indicating that p is a pointer // & I-representing the address of the variable I. The "&" Here is the address operator, indicating the address int * p = & I; // * p-representing the content pointed to by the pointer p. The "*" here is the content operator, indicating to take the content pointed to by the pointer p int j = * p;} // string pointer void pointer_demo2 () {// define a string pointer (the string ends with '\ 0') char * p = "I am webabcd"; // p is the string pointer, the result pointed to by p + 5 is webabcd char * name = p + 5; // String length int length = strlen (name); // 7, excluding '\ 0' // memory space occupied by the pointer int memory = sizeof (name); // here I am a 4-byte char * p_address = (char *) malloc (32); sprintf (p_address, "% p", name); // address value of the pointer name. Here I am a 32-bit address, similar to 6416C99D free (p_ad Dress) ;}// pass the pointer as a parameter void pointer_demo3 () {int m = 1; int n = 2; int * x = & m; int * y = & n; // After swap is called: * x = m = 2, * y = n = 1 void swap (int * I, int * j); swap (x, y );} void swap (int * I, int * j) // exchange the integer values pointed to by two pointers {// The form parameter is a copy of the real parameter, and the memory unit is allocated during the call, release immediately after the call ends // This is also true for pointers, except that the copy address is used as the copy int temp; temp = * I; * I = * j; * j = temp ;} // array pointer operation void pointer_demo4 () {int ary [] = {11, 22, 33, 44, 55, 66}; // because ary is a number The first address of the group, so ary = & ary [0] if (ary = & ary [0]) {// ary is the first address of the array, that is, the address of the first element of the array. After the content is obtained, it is the content of the first element of the array. The result is 11 int x1 = * ary; // The array pointer is operable, it is used to calculate the position index of an element in an array. For example, if you use ary + 2 as an example, the pointer to the first element of the ary array. If you add 2, it is the pointer to the second element of the array, the result is 33 int x2 = * (ary + 2); char * p0_address = (char *) malloc (32); sprintf (p0_address, "% p ", ary); // if the pointer value is 0535DC30, free (p0_address); char * p1_address = (char *) malloc (32); sprintf (p1_address, "% P", ary + 1); // If the integer is 4 bytes, the pointer value here is 0535DC34 free (p1_address );} // another example of pointer operation (copy string str1 to string str2) char str1 [] = "I am webabcd", str2 [20], * p1, * p2; p1 = str1; p2 = str2; for (; * p1! = '\ 0'; p1 ++, p2 ++) * p2 = * p1; * p2 =' \ 0'; p1 = str1; p2 = str2; // return the first address of the array} // void pointer_demo5 () {int ary [2] [6] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // defines a pointer to a two-dimensional array: type specifier (* pointer variable name) [number of columns, that is, two-dimensional length] int (* p) [6] = ary; // p is ary if (p = ary) {// here we need to make a good understanding (take the pointer to understand, ary is equivalent to a second pointer, ary [I] is equivalent to a first-level pointer) // ary [1] is the first address of a one-dimensional array with 2nd rows of two-dimensional arrays, that is to say, it is equivalent to & ary [1] [0] // ary + 1 pointing to ary [1], and its content * (ary + 1) is ary [1 ]. // Ary [1] = & ary [1] [0] = * (ary + 1) // further understand that ary is the first address of a two-dimensional array, * ary is the first address of a one-dimensional array of 1st rows in a two-dimensional array, * (ary + 1) is the first address of a one-dimensional array of 2nd rows in a two-dimensional array // fetch 2nd rows, 6th columns of data, the result is 11 int x = p [1] [5]; // further understand it. // p is a pointer to a two-dimensional array. // p + 1 points to ary [1], so * (p + 1) that is, ary [1] // * (p + 1) + 5 points to p [1] [5], so * (p + 1) + 5) p [1] [5]. The result is 11 int y = * (p + 1) + 5 ); // ary [I] = * (ary + I), ary [I] + j = * (ary + I) + j, ary [I] [j] = * (ary + I) + j)} // Function Pointer (pointer to function) void pointer_demo6 () {// define a function pointer: type specifier (* pointer variable name) (); int (* pmax) (int, int B); // Let the function pointer point to the function my_max int my_max (int a, int B); pmax = my_max; int x = 10, y = 100; // (* pmax) Is the function my_max, and the result is 100 int z = (* pmax) (x, y); // Of course, the function pointer is also a pointer, you can obtain its address char * p_address = (char *) malloc (32); sprintf (p_address, "% p", & pmax); free (p_address );} int my_max (int a, int B) // obtain the maximum values of two integers {if (a> B) return a; return B;} // pointer type function (function return value can be a pointer) void pointer_demo7 () {// Add the "*" sign before the function name to indicate that this is a pointer type function, that is, the returned value is a pointer. The type specifier indicates the data type char * day_name (int n) pointed to by the returned pointer value; // result: char * result = day_name (5) on Friday );} char * day_name (int n) {static char * name [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Friday ", "Saturday"}; return (n <0 | n> 6 )? "Unknown": name [n]);} // pointer array void pointer_demo8 () {char * str1 = "webabcd"; char * str2 = "wanglei "; // define a pointer array, that is, each element in the array is a pointer char * p [2] = {str1, str2 }; // both str1 and str2 are pointers // The result is wanglei char * x = p [1]; // p [1]-2nd pointers in the pointer array} // void pointer_demo9 () {// the pointer array is actually the pointer char * name [] = {"webabcd", "wanglei "}; // define the pointer char ** p = name; p ++ through "**"; // The result is wanglei char * myName = * p; // p is the pointer (second-level pointer), * p is the pointer (first-level pointer)} // void type pointer void pointer_demo10 () {void * void_pointer (); // void pointer-the data type in the memory must be specified by the user. char * name = (char *) void_pointer ();} void * void_pointer () // return the void pointer {return "wanglei ";}
OK
[Download source code]