C language pointer, a dynamic array, pre-compiled summary

A pointer:

For the pointer, I will not introduce the concept of a lot of textbooks speak better than me. I am here to summarize the point pointer operation should be noted:

1. During transfer function parameters, an array of pass-by, other types of traditional values, local variables, but to other pass-through function to change the value.

    The int a = 1; the function parameter should write int * x; when the parameter passing arguments be & a;

    However, when the pass-by, a function of the parameter change point, without affecting the original function arguments directed.

    Original function arguments such as a-> Address 1, pass arguments parameter b-> Address 1, the b value does not affect a change point.

2. The two-dimensional array under the circumstances, there is the relationship of FIG. (FIG appreciated that this pointer operation mention basic): 

    

3. The two-dimensional array as a parameter of three ways: namely, int b [] [6], int (* b) [6], int ** b. Wherein the first two-dimensional can not be omitted, obtained after the third parameter passing is actually a one-dimensional array, Note operation mode.

4. c string stored in two ways: the first is an array of characters, but the character pointer. Manner but the two stored string is different, each character array unit storing a value, stored in the pointer and the character string is only the first address. Compile the system when only a 4-byte character pointer for storing an address space, and the character array is a series of contiguous storage space value.

For character pointer, it can be made directly assigned as c ++ kinds of string type, puts () output, as an array of characters as standard access under use, can be assigned and character pointers by an equal sign, but use the character pointer must remember to write the initial value, otherwise the pointer points to is uncertain, the use of wild pointers is dangerous. Of course, when using a character pointer, if it intends to initial values, it may be incorporated into a dynamic array open some space to address to the character pointer, but be careful this time the character pointer address has changed.

For possible between assignments, character pointer equal sign between the characters. But for the character pointer and a character array b, we can not use the equal sign is the first natural, and secondly for the character copy function, strcpy (a, b) No, strcpy (b, a) you can (this should be related to the compiler theory), to perform a first operation, it should be written as a = & b [0] or a = & b, or open up to a certain space.

5. pointer function: First, the function name is the first address, name of the function is a function pointer. If a function int max (int a, int b ); p is a pointer to this function should be defined as int (* p) (int, int), then p = max. When you call (* p) (a, b ) can be.

To return a pointer, a function type can be modified accordingly.

6. The array of pointers: char * name [] = { "AAA", "BBB", "CCC"}; element name are stored in three addresses three first string, puts (name [i]) ; output respectively three strings.

7. pointers to pointers : char * name [] = { "aaa", "bbb", "ccc"}; char ** p; p = name; puts (* (p + i)); three output respectively string.

Array of pointers and pointers to pointers role in a string of relatively straightforward and similar to other cases, but is currently experiencing the practical application of these situations, after the encounter to add.

Second, the dynamic array:

1. int * p; p = (int *) malloc (sizeof (int)); // int open space of a size, which is cast to an int type pointer address to p.

2. int * p; p = (int *) calloc (5, sizeof (int)); // int open five consecutive space size, its address is first cast an int pointer to p.

2. free (p); // release the application space, does not release easily lead to memory leaks.

Third, the preprocessor:

1. The macro definition: #define PI 1e6 + 5

2. The file contains: file system: #include <stdio.h>, custom file #include "gaoithe.h".

3. conditional compilation:

//第一种:如果标识符被定义,执行程序段1,否则执行程序段2

#ifdef 标识符
    程序段1
#else
    程序段2
#endif

//第二种:表达式值为真,执行程序段1,否则执行程序段2

#if 常量表达式
    程序段1
#else
    程序段2
#endif

4. Supplement a similar but not the preprocessor, i.e. replaced variable name: typedef long long LL;

Published 34 original articles · won praise 26 · views 10000 +

Guess you like

Origin blog.csdn.net/sinat_40471574/article/details/90213740