First, the pointer Prelude 1. The importance of pointers
Pointers are a very important type of data in C, and if you say that you have a good study in c except for pointers, you simply say you have not learned C language.
2. Small needs
l void Change (int n) after the function call is complete, the value of the argument is changed
L Analysis: Change the value of the argument, find storage space , address
Second, the pointer variable definition 1. The format of the definition
L class name identifier * pointer variable name;
l int *p;
2. Define the Post assignment first
L Simple Value
int a = 10;
int *p;
p = &a;
printf ("%d", *p);
L Simple Change value
*p = 9;
3. Assigning values at the same time as defined
int a = 10;
int *p = &a;
4. Implement modify argument 5. Watch out.
l int *p; p = 1000;
l int *p; *p = 100;
l int *p; *p = &a;
L%p The address value stored in the output pointer
L Other pointer type description, such as float *p; char *p;
L can not use the type, such as int a = 10; float *p = &a;
6. Clear the pointer
L p = 0;
L p = NULL;
Third, pointer instances
- void Swap (char *a, char *b) (note temp=a; A = b; b = temp; )
- int Sumandminus (int a, int b, int *minus)
Iv. the research of pointers
- Storage space occupied by pointer variables
- Why are pointer variables typed?
int i = 2;
char c = 1;
int *p = &c;
printf ("%d", *p);
V. Pointers and Arrays 1. Pointer to a one-dimensional array element 2. Traversing one-dimensional array elements with pointers
L Iterate through the char array (' i ', ' t '), and then iterate through the array of int types
the difference between L * (p+i) and * (p++)
L a+i and a++
L p[0],p[1]
3. Exercises
1> Designs a function:int arraysum (int a[], int n), and the number of the first n ofa one-dimensional array . Now replace int a[ with int *p ]
Six, pointers and strings 1. String review
Char s[] = "MJ";
2. Other ways to define strings
Char *s = "MJ";
Or
Char *s;
s = "MJ";
3. The difference between the two ways of definition
L Memory Analysis
L Drawing Analysis
L The difference between a constant and a variable string
L Constant Memory address view
4. Exercises
1> writes an int string_len (char *s)that returns The character length of the string s
Functions for returning pointers
The pointer is also a data type in C, so the return value of a function can certainly be a pointer-type
L The general form of a function that returns a pointer is: type name * Function name ( parameter list )
Eight, pointer to function 1. Why can pointers point to a function?
function as a program, in memory also occupy a portion of storage space, it also has a starting address, that is, the entry address of the function. function has its own address, that's good, our pointer variable is used to store the address. Therefore, a pointer can be used to point to a function. Where the function name represents the address of the function.
2. Definition of pointers to functions
General form of definition: The return value type of the function (* pointer variable name ) ( parameter 1, parameter 2, ...);
3. Use note
L because this type of pointer variable stores the entry address of a function, it makes no sense to add or subtract them (such as p++)
The pointer variable that points to a function has two main uses:
- Calling functions
- Passing functions as arguments between functions
Pointers in C