1, one-dimensional array pointers
Pointer to a one-dimensional array
int a[5] = {1,2,3,4,5};
int *p = A;
2, two-dimensional array pointers
Pointer to a two-dimensional array
int arr[1][3] = {n/a};
int (*p) [3] = arr;
Attention:
The array name A does not represent the entire array, but represents the address of the first element of the array.
3. Array of pointers
Array of pointers: Each element of an array is a pointer
or an array of pointers, that is, an array of pointers.
int a=3,b=4,c=5;
int *pa[3]={&a,&b,&c};
int a[2][3]={{1,2,3},{4,5,6}};
int *pa[2]={a[0],a[1]};
4. Operations between pointers
Prerequisite: Two pointers must point to the same array
Arithmetic: Subtraction
Essence: Calculates the relationship between two pointers and determines the position of the pointer
p = A; P1 =&a[3]
P1-p > 0 means p1 in high position
P1-p < 0 means p1 in low position
P1-p = 0 means that P1 and P point to the same position
Attention:
The operation of a pointer cannot be added, multiplied, or removed.
5. Array name access to two-dimensional arrays
int A[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
The first element address that accesses the first line represents:
A + 1 = &a[1] = a[1] = &a[1][0] = * (A + 1)
Gets the first element value of the first row in a way:
*A[1] = a[1][0] = * * (A + 1)
6. Pointers and strings
1, string pointer, used to save the string
Char *s = "AGADSF"; There are
2. Save string with character array method
Char ch[]= "DAFASDF";//
3. The difference between a string pointer variable and a character array
A, difference one, can re-assign the problem
Char ch[20]= "xxxxx";
ch = "ASDFASDF"; No, CH is a constant, storing the first address of an array.
Char *ss= "xxxxxx"; "XXXXXX" 0x01
SS = "XXX"; Yes, the SS is a pointer variable,
b, difference two, the difference between storage
Char ch[10]= "XXXX"; Storage is stack area (readable and writable) x x x x 0 0 0 0 0 0 0
ch[4]= ' A '
Char *str = "PPPP"; Store a constant area (read-only)
7. How strings are stored
Several ways to store strings
1. One-dimensional character array
Char ch[10] = "Fengjie";
2. Two-D character array
Char name[10][20]={
"Fengjie",
"Suibian",
"XXXXXX"};
3. String pointer variable
Char *str = "Fengjie";
4. Pointer array constant area
Char *name[10]={
"Fengjie",
"Suibian",
"XXXXXX"
}
I. Basic points of knowledge
Int a=10;
int *p;//defines a pointer of type int
p=&a;//pointer variable p points to the variable a
*p=20;//the value of variable A is 20 by using the pointer without directly modifying the variable
*p represents access to the storage space that the pointer variable p points to
Pointer one function: Ability to access (value |) According to an address value Assignment) corresponds to the amount of storage space
int in front of pointer variable p, indicating the type of pointer
①. Int *p;
②. *p=10;
Two * difference: the previous function, indicating that the defined p is a pointer, the latter's * represents the address space pointed to by the access P
Second, the use of pointers note
①. Int *p;
Double d=10.0;
p=&d;//does not recommend this approach
②. Int *p;
p=200;//pointer variable can only store address
③. Int *p;
printf ("%d\n", *p);//pointer variable uninitialized, do not use to indirectly access other storage space
④. int *p=&a; but cannot be written as int *p;*p=&a; this notation has no meaning and can be thought of as being used with a type character.
⑤. * is a pointer operator that accesses the space pointed to by the pointer
Third, pointer pointing pointer
Int a=10;
int *p=&a;//pointer to type int
INT **p1=&p;//Pointer to pointer
Int ***p2=&p1;//Level Three pointer
*P2 equivalent to visiting P1;
**P2 equivalent to access p;
P2 equivalent to access A;
*P1 equivalent to access p;
A star and a line.
Four, hands exercise
Write a function that calculates the and difference between A and B (a function returns two values)
Tip: One of the pointers: implementing a function that has multiple return values
View Code
V. Questions relating to pointers
Note: If a pointer of any type occupies 8 bytes of storage space, why do you add a type to the pointer?
A memory analysis of the following code can prove the serious consequences of incorrect pointer type.
Int i=2;
Char c=1;
int *p=&c;//should be of type char, written in int type
Printf ("Value of C is%d\n", *p);//print result is 513, not 1
Printf ("C's value is%d\n", c);//value is 1
The following is a memory analysis of the results of the above code:
The pointer p is supposed to be 1 bytes of data, because the pointer is of type int, so the program naturally starts reading 4 bytes of data from the point of Address 0x0a, and the data accessed from 1 becomes 513.
Tip: The data type of the pointer is clarified, and the pointer is able to correctly access the spatial data that should be accessed.
Vi. Pointers and Arrays
Int ages[5]={10,9,8,7,6};
Iterating through an array
for (int i=0;i<5;i++)
Printf ("%d\n", Ages[i]);
Iterating through an array using pointers
Int *p;
p=ages;//can also be written as p=&ages[0]; the pointer variable p points to the first element of the array
The address of the element:
Address of the first element P &ages[0]
Address of the second element p+1 &ages[1]
Address of the third element p+2 &ages[2]
The value of the element
*p Ages[0]
* (p+1) ages[1]
* (p+2) ages[2]
Use the pointer as an array:
for (int i=0;i<5;i++)
Printf ("%d\n", * (P+i));
(1) Three types of access to array elements:
①. Array name [subscript]
②. Pointer variable name [subscript]
③. * (p+1)
(2) How much does +1 of the pointer variable actually add? This depends on the type of pointer, plus 1 bytes if it is a char type, or 4 bytes if it is of type int.
(3) The pointer is used to receive an array, and the pointer variable points to the first element of the array.
void change (int array[]) is equivalent to void change (int *array).
The former is stored as the first address of an array element, but it becomes a pointer when it is passed.
Example:
Void change (int *array)
{
Printf ("%d\n", array[2]);
Printf ("%d\n", * (array+2));
}
Int Main ()
{
Int ages[5]={1,2,3,4,5};
Change (ages);
}
The result of the call is: the third element of the array 3
If changing to change (&ages[2]), the result of the call is 5, because at this point the array points to ages[2] and ages[2] as the first element of the array.
Vii. Pointers and strings
(i) Basic knowledge
The following two lines of code are essentially different:
①. Char name[]= "it";
②. Char *name2= "it";//pointer variable name2 points to the first character of the string I
Char name[0]= ' y ';//change the value of the first element
Printf ("%s\n", name);//print result for YT
*name2= ' Y ';
Printf ("%s\n", name2);//This time the program crashes
This is because one of the two is a string variable, and the other is a string constant. The C-language array elements are stored on the stack, and the elements inside can be arbitrarily modified, called string variables. The string constants are stored in the constant area and are not changed by the cache.
Char *name1= "it";
Char *name2= "it";
Printf ("%p%p", name1,name2);//The address is the same, stating that name1 and Name2 point to the same string.
There are two ways to master the definition of a string:
①. Using arrays
Features: The characters inside the string can be modified, which is useful when the content needs to be modified frequently.
②. Using pointers
Features: In fact, is a constant string, the characters inside cannot be modified, applicable to the content of the string does not need to be modified, and this string is often used.
(ii) array of pointers
Integer array: This array is an array of integers
Pointer array: This array contains pointers.
Int Ages[5];
Char *name[5]={"Jack", "Rose", "Yang"};//a common notation for string arrays
Corresponds to:
Char name2[3][10]={"Jack", "Rose", "Yang"};
There are two ways to save an array of strings:
①. Array of pointers (array of strings)
②. Two-D character array (array of strings)
How do I enter a string? (using arrays-because they are mutable)
Int Main ()
{
Char NAME[20];
Printf ("Please enter name: \ n");
SCANF ("%s", name);
Printf ("%s", name);
}
Functions for returning pointers
Examples of programs:
#include <stdio.h>
Char *test ();
Int Main ()
{
Char *name=test ();
Printf ("name=%s\n", name);
Return 0;
}
Char *test ()//function to return pointer
{
Return "Rose";
}
Ix. Pointers to functions
The array name is the address of the array, and the function name is the address of the function.
Suppose there are functions:
Void Test ()
{
Printf ("Call the test function \ n");
}
Void (*p) (); The function that the void pointer variable points to does not have a return value, () indicates that the function P points to has no formal parameter
P=test; Pointer p, pointing pointer p to function
There are three ways to manipulate functions:
①. Call test directly ();
②. Call with pointer variable introduction (*P) ();
③. Simplified use P ()
Practice:
Assume that a function is declared as int sum (int a,int b)
The corresponding pointer to the function should be defined as: Int (*p) (int, int);
Point pointer variable p to function: P=sum;
There are three ways to call this function:
(1) int c=p (10,12);
(2) Int c=sum (10,12);
(3) Int c= (*p) (10,12);
Suppose the function is declared as: double haha (double a,char *b,int c);
Then define a pointer to the Haha function should be: double (*p) (Double,char *,int) =haha;
C Language-pointers