1. A little base int * p = null; defines P is a pointer. P This pointer is 4, which is P, and the memory address of the P will add 4 BYTE. The initial value of P is NULL. The definition of NULL is generally (void *) 0 or 0. If P is global variable, then the memory unit is assigned in a static storage area at LINK; if it is a local variable, the memory unit is allocated at runtime. On Stack. When defining a pointer, the compiler does not assign space to the object pointing to the pointer, which is just a space to assign a pointer itself unless initialized by the pointer at the same time. EG.
Char * p = "bowfruit"; Note that only the string constant is true. In addition, the string constant created when the pointer is initialized in ANSI C is defined as read-only. If you try to modify the value of this string through a pointer, the program will appear undefined behavior. This string is often stored in a data segment, but in some compilers, stored in the text segment to prevent it from being modified. Contrary to the pointer, an array initialized by a string constant can be modified. [Parts Are Referred from << C Expert Programming >>]
2. Common usage (1). Access memory data. From the bottom view, a pointer is a memory address. (2). When dynamic allocation, store allocated memory addresses. EG.
Char * p = null; p = (char *) malloc (MAX_MEM_SIZE); (3). In the function, it is incorporated or transmitted, and the incoming data EG is accessed within the function.
INT gid_name (char * name, gid_t * gid) {[...] * gid = PTR-> gid = GR-> gr_gid; return (0);} The return value is only used to mark the error condition. The use of pointer type parameters is sometimes also to avoid a large amount of data COPY when the function call is called, such as structures, and the pointer will be more efficient. EG. Static double
Diffsec (struct timeval * now) {return ((now-> TV_sec - then-> tv_sec) * 1.0 (now-> TV_USEC - THEN-> TV_USEC) / 1000000.0);} The function is passed The result of the members of the two structures. As mentioned above, when the parameters incorporated by the pointer do not need to be modified, finally add the const keyword to the front, indicating that the parameter is read only in the function scope. (4). Data members Access members of the access structure: P-> F; Access array elements:
P = & a [0]; * (p 3) = 0; (5). Alternative array as a function parameter in C, when the array name is used as a function parameter, the incoming actual is the first element of this array. The address, that is, it is noted that when the array is transmitted as a function of the parameter, the array automatically degenerates into the same type of pointer. At this time, any change in the array in the function will directly affect the array itself. Similarly, the number of returns in the C function also returns only a pointer to the first element of the array. So, when returning from the function, make sure that the array is not a local variable defined in a function, local variable memory is allocated on the stack, otherwise, once the function exits, the content of this array will be overwritten, returned data So it is invalid. A way to avoid this is to define the array as static. Eg.char * inet_ntoa (struct in_addr ad) {unsigned long int s_ad; int A, b, c, d; static char addr [20];
s_ad = ad.s_addr; d = s_ad% 256; s_ad / = 256; c = s_ad% 256; s_ad / = 256; b = s_ad% 256; A = S_AD / 256; SPRINTF (ADDR, "% D.% d.% d.% D .% d.% D ", A, B, C, D); Return addr;} If the function is not stated as static, when the array addr is static, the content of the ADDR array will be invalid when the function inet_ntoa () return. (6). Function pointer C does not allow functions to be incorporated into other functions as parameters, but allowing a function pointer to another function. (7). As an alias EG. Struct
Output output = {null, 0, null, outbufsiz, 1, 0}; [...] Struct
Output * Out1 = & output; In the above example, the pointer variable OUT1 can be used in places where the variable Output is used. It is generally used for three reasons: i. Improve efficiency If a data structure is relatively large, in some occasions, such as assigning statements, using pointers to avoid copies of the entire structure.
Static struct termios Cbreakt, Rawt, * Curt; [...] Curt = UseRaw? & Rawt: & CBreakt; II. Accessing Static's data is often used to point to different static data, eg. char * s; [...] s = * (OPT-> BVAL)? "true": "false"; III. Access the same global data EG. Static Long RNTB [32] = {3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, [...]};
Static long * fptr = & rNTB [4]; [...] void srrandom (int x) {[...] fptr = & stat to [rand_sep]; [...]}
Long rrandom () {[...] * fptr = * rptr; i = (* fptr >> 1) & 0x7ffffff;
} In this example, FPTR is initialized to a random number of seed tables, and then assign values again in the function srrandom (), and finally, in the RRANDOM function to modify the pointed data.
(8). Handling string, such as Strlen functions:
SIZE_T STRLEN (const char * str) {register const char * s
For (s = STR; * S; s); Return (S - STR);} (9). Direct memory Access Since the pointer is characterized by a memory address, the pointer will be used in the underlying code to access specific specific Hardware memory address. Static void vid_wrchar (char c) {volatile unsigned short * video = null;
Video = (unsigned short *) (0xE08B8000) VID_YPOS * 80 VID_XPOS; * Video = (* Video & 0xFF00) | 0x0F00 | (unsigned short) C;} Note that modern operating systems generally do not allow direct hardware access, so general Such code only appears in an embedded system or kernel and driver.
3. Using rules suggesting that these rules are mainly to prevent memory leakage (1). After using Malloc, it should immediately check if the pointer value is NULL. Prevent memory using the pointer value of NULL. Char * p = (char *) malloc (user_define_size); if (null == p) {/ * exception handle * /} (2). A initial value in array and dynamic. Prevent memory that will not be initialized as the right value. For array: int avar [100] = {0};
For Dynamic Allocate Memory: char * p = null; / * initial * / p = (char *) malloc (user_define_size); / * malloc buffer * / if (null == p) {/ * exception handle * /} MEMSET P, 0, user_define_size); (3). Avoid the subsidiary of the array or pointer, especially if "more 1" or "less 1" operation occurs. (4). Application and release of dynamic memory must be paired to prevent memory leakage. For Free Allocate Memory: if (null! = P) {free (p);} (5). After free memory, the pointer is set to NULL to prevent "wild pointer". [Reference from << High Quality C / C Programming Guide >>]