void ChangeValue (int num1,int num2)
{
int temp = NUM1;
NUM1 = num2;
num2 = temp;
}
The pointer is passed as a function parameter, which can be changed to the actual parameter.
void changeValue2 (int *p1,int *p2)
{
int temp = *P1;
*P1 = *P2;
*P2 = temp;
}
character pointers as formal parameters
void Func2 (int *a,int count)
{
for (int i = 0; i < count; i + +)
{
printf ("%d", * (A+i));
}
printf ("\ n");
}
int main (int argc, const char * argv[]) {
/*
1. When the program runs, the system allocates memory units for the tax bureau and stores the data.
2. Memory is made up of multiple contiguous memory units
The memory cells are used to store data, each of which occupies one byte, one byte = = eight bits
Memory Unit Address: Number of memory units, contiguous. Easy access to memory units, just like house numbers. (Fig. 2)
3. How the data is stored (Figure 3)
4. Two ways of accessing data;
(1) Direct access. Define variables to store data, Access data by variable name/
int a = 10;
int b = A; Accessing in-memory data through variable a
(2) Indirect access/through memory unit address, access to in-memory data, any value is stored in memory in the storage unit.
5. The address of the memory cell is called the pointer
Pointer variables: variables that store pointers (addresses)
Define a pointer variable
Int*p = NULL;
float *q = NULL;
char *r = NULL;
NULL is a null pointer; equivalent to 0. Null is the value assigned to the pointer variable.
Syntax for defining pointer variables
Type modifier * pointer variable name = initial value (address)
* means: The defined variable is a pointer variable and the stored content is the address.
Type modifiers represent: The type of data in the storage unit to which the address points (that is, the type of data found by the address)
Type of pointer variable: type modifier * pointer type pointing to a type of data
Type of p int * pointer type pointing to integer data
Type of q float * pointer type pointing to floating-point data
Type of R char * pointer type pointing to character data
Pointer variables are often referred to as pointers
P-Integer pointer
Q Floating-point pointer
R-Character Pointer
To access a storage unit through a pointer, you must first obtain the address of the storage unit
Take the address operator & get the address of the variable
Output Address%p
int a = 100;
printf ("Variable A's storage unit address:%p\n", &a);
float B = 1.5;
printf ("Variable B's storage unit address:%p\n", &b);
char c = ' a ';
printf ("Variable C's storage unit address:%p\n", &c);
printf ("-----------------------------\ n");
Defining the pointer storage address
Requirement: type modifiers for pointers and types of variables pointed to by pointers must be consistent
int *p = &a;
(int *) p = &a; The pointer p points to the storage unit of variable A (the address stored in the pointer p is the storage cell address of variable a)
printf ("Address stored in pointer p:%p\n", p);
float *q = &b;//* When defining pointers, it only acts as a modifier, stating that I define a pointer variable
printf ("The address stored in pointer Q:%p\n", q);
Char *o = &c;
printf ("The address stored in the pointer o:%p\n", O);
printf ("-----------------------------\ n");
accessing storage units with pointers
The value of the storage unit that accesses the variable
printf ("%d\n", *p); *p gets the value in the memory address that P points to
int i = *p; i = A;
float j = *q; j = b;
char k = *o; K = c;
printf ("i =%d j =%.2f k =%c\n", I, J, K);
Use the number of modifications
A = 200;
b = 2.5;
c = ' B ';
printf ("a =%d *p =%d \ n", A, *p);
Using pointers to modify
*p = 300;
*q = 3.5;
*o = ' C ';
printf ("b =%.2f *q =%.2f \ n", B, *q);
& and * are compatible with each other for inverse
& Get variable Storage cell address
* Storage unit for accessing variables via address
Practice
int x = 0;
int y = 0;
int *m = NULL; Pointer m points to the null pointer position
m = &x; Pointer M re-points to variable X
*m = 10; *m accesses the storage unit of the variable x through the address of the M store, reading the value of X
printf ("%d", *m);
m = &y;
*m = 20;
printf ("%d", *m);
The storage space of the pointer is related to the operating system
4 bytes in 32-bit operating system, 8 bytes in 64-bit operating system
printf ("%lu\n", sizeof (m));
Distinguish the role of * in a pointer
1. If the pointer variable is defined, int *p = NULL; This * indicates that p is a pointer variable.
2. If the definition is complete and then uses *p, the * represents the data in the address where P is taken. is the value operator
Operation of Pointers
Pointers only add and subtract operations
The type that defines the pointer determines how many characters the pointer moves to
First type: P + N
Represents: The number of bytes from the storage unit that P points to the high position of n data types (n * bytes of data type)
The direction of P has not changed
int a = 100;
int *p = &a;
printf ("%p", p);
printf ("%p", p + 1);
printf ("%p \ n", p + 2);
The second type: p-n
Represents: The number of bytes from the storage unit that P points to the low offset n data type (n * bytes of data type)
The third type: P + + or + + P
The pointer moves to the high address, and the distance it moves is the number of bytes that the pointer points to the data type
P's direction changed.
int a = 200;
int *p = &a;
printf ("%p", p);
printf ("%p", p + +);
printf ("%p", + + p);
*/
/*
1. Define an array
int a[5] = {1,2,3,4,5};
2. Access array element array name [subscript]
A[0] = 10;
printf ("%d\n", a[0]);
3. Calculating the number of array elements
printf ("%lu\n", sizeof (a));
printf ("%lu\n", sizeof (int));
printf ("%lu\n", sizeof (a[0]));
printf ("%lu\n", sizeof (a)/sizeof (int));
printf ("%lu\n", sizeof (a)/sizeof (a[0]));
4. The array name is constant, indicating the address of the first element of the array a = &a[0]
#pragma mark--------pointers and Arrays------------
Pointers can point to variables, read values in variable storage cells
Pointers can point to array elements, which are contiguous storage spaces in memory, with each element occupying the corresponding storage unit.
The pointer to an array element is the address of an array element
Access to array elements can use array names or pointers to arrays
Two ways to access array elements: Subscript method, Pointer method
1. Subscript method
int a[5] = {1,2,3,4,5};
int *p = A; The array name is constant, which represents the address of the first element of the array &a[], at which point P points to the storage unit of the first element of the array, representing the first element address.
Using the array name
printf ("%d%d\n", a[0],a[1]);
printf ("%d%d\n", p[0],p[1]);
Using pointers
for (int i = 0; i < 4; i + +)
{
for (int j = 0; J < 4-i; J + +)
{
if (P[j] < P[j + 1])
{
int c = 0;
c = A[j];
P[J] = p[j + 1];
P[j + 1] = C;
}
}
}
for (int i = 0; I <5; i + +)
{
printf ("%d", p[i]);
}
2. Modify pointer pointing, cannot use array name, only use pointer;
printf ("%d \ n", *p); Point to A[0]
Modify equivalence
int b[5] = {1,2,3,4,5};
int *q = b;
B[1] = 20;
Q[1] = 30;
* (q + 1) = 10;
* (b + 1) = 20;
for (int i = 0; I <5; i + +)
{
printf ("%d", b[i]);
}
The difference between a pointer and an array
Pointers and arrays can access array elements through subscript and pointer methods
1. Pointers can be modified to point to
The array name is a constant that represents the address of the first element and cannot be changed
2. The storage space of the pointer; 4/8 bytes
Storage space for arrays: number of elements * storage space for elements
3. sizeof (array name) gets the storage space of the array
sizeof (the pointer) gets 4/8 bytes, regardless of what kind of data is stored in the storage space the pointer is pointing to.
Char a[3] = {' A ', ' B ', ' C '};
char *f = A;
printf ("%lu\n", sizeof (f));
What happens to the pointer type and array element type mismatch
Short A[4] = {6,7,8};
int *p = A;
printf ("%d\n", *p);
*/
#pragma mark----------pointer and string-----------
/*
Defines a character array store string, str is stored in the stack, a copy of a constant string is stored in a character array, and the elements in the array can be changed.
Char str[] = "Hello";
char *p = str;
Modifying array elements with pointers
*p = ' a ';
Using pointers to ask array elements
for (int i = 0; i < 6; i + +)
{
printf ("%c", * (P+i));
}
printf ("\ n");
Manipulating strings with pointers
printf ("%s\n", p);
Defines a character pointer to a string
"" Constant string, stored in a constant area, can only access cannot be modified
Defines the first address of a string constant pointing to a constant area, where the pointer p is just the storage address
char *p = "Hello";
*p = ' a ';
can access characters and strings
printf ("%s\n", p);
printf ("%c\n", *p);
Summary: Pointers to arrays can access and modify array elements; Pointers to constant strings are only accessible and cannot be modified.
Exercise: Calculating string lengths with pointers
Char str[] = "iphone";
char *p = str;
int n = 0;
while (p[n]! = ' + ')//* (p+n)! = ' "
{
n + +;
}
printf ("%d\n", N);
*/
#pragma mark-------------pointer array---------------
/*
An array of two-dimensional arrays that store string arrays
Char strs[3][5] = {"ios", "ipad", "imac"};
STRs stored array elements are character arrays (strings stored in character arrays, copies of Constant strings)
printf ("%s\n", Strs[0]); Strs[0] The first element address of an array of characters. You can access the character array
strcpy (Strs[0], "Mac");
printf ("%s\n", Strs[0]);
The elements in an array of pointers are pointers
Char *strs[3] = {"IOS", "IPad", "IMac"};
STRs Essence is a one-dimensional array
STRs stored array elements are character pointers (character pointers point to constant strings)
STRS[0] First character pointer, pointing to the constant area of "IOS"
STRS[1] Second character pointer, pointing to the constant area of the "IPad"
Strs[2] The third character pointer, which points to the "iMac" of a constant area
Char *strs[3] = {"IOS", "IPad", "IMac"};
printf ("%s\n", *strs);
printf ("%s\n", * (strs+1));
Modify
Strs[0] = "Symban"; Strs[0] Accesses a character pointer, and the pointer re-points to another constant string
printf ("%s\n", Strs[0]);
*/
Pointers and function pointers as arguments to functions
int NUM1 = 30;
int num2 = 90;
ChangeValue (NUM1, num2);
printf ("%d%d\n", num1,num2);
ChangeValue2 (&NUM1, &num2);
printf ("%d%d\n", num1,num2);
Character Pointers as arguments
int *p = A;
Func1 (p,5);
Func2 (P, 5);
C Introductory Section Eighth pointers