Learning notes
Pointer:
- Importance of Pointers:
- Represents complex data structures
- Express delivery data
- Causes a function to return more than one value
- Direct access to hardware
- Easy to manipulate strings
- Pointers are the soul of C
- Pointer definition:
#include <stdio.h> int main() { int *p; //p is the name of the variable int * is the address of the variable int I = 3; int j; p = &i; // P is not I, and I is not p. To be exact, changing the values of p and I does not affect the values of both. // If a pointer variable refers to a common variable, the * pointer variable is exactly the same as the common variable: *p = I; *p = I; *p = I; // that is j = I; printf("i = %d, j = %d\n", i, j); return 0; }Copy the code
- Address:
- Number of a memory unit
- A non-negative integer starting from zero
- Range: 4G (0–4G-1)
- Pointer:
- A pointer is an address, and an address is a pointer
- A pointer variable is a variable that holds the number of a memory location, or a pointer variable is a variable that holds the address
- Pointers and pointer variables are two different things, and although we often refer to pointer variables as Pointers, they don’t actually mean the same thing
- The essence of a pointer is a non-negative integer with limited operation
- Classification of Pointers:
*
Primitive type pointer- Pointer common error:
#include <stdio.h>
int main(void)
{
int i = 5;
int *p;
int *q;
p = &i;
p = q; //q is garbage, assigned to p, p also becomes garbage
printf("%d\n", *p);/*q space belongs to the program, so the program can read and write the content of q, but if the internal value of q is garbage, the program can not read and write the content of *q, because the control permission of the memory unit represented by *q has not been allocated to the program, so the program will fail to run until line 11 */
return 0;
}
Copy the code
The results:
Swap two numbers:
#include <stdio.h>
void f(int *p, int *q)
{
int t; // If *p and *q are to be interchanged, t must be defined as int, not int *, otherwise the syntax will fail
t = *p; //p is int *, *p is int
*p = *q;
*q = t;
}
int main(void)
{
int a = 3;
int b = 5;
f(&a, &b);
printf("a = %d, b = %d\n", a, b); //f(*p, *q) is an error, and f(a, b) is an error
return 0;
}
Copy the code
The results:Note:
* Meaning:
1. The multiplication
Int *p; // define a variable named p, intIndicates that p can only hold variables of type int
3. Pointer operator: This operator is placed before a defined pointer variable, if p is a defined pointer variableP saidTake the contents of P as the variable of the address.
How to change the value of the main function’s ordinary variable through the called function:
1. The argument must be the address of the common variable
2. Parameters must be pointer variables
3. Pass the * parameter name =…… in the function to be called You can modify the values of variables associated with the calling function
- Pointers and arrays
Int a[8]={1,2,3,4,5,6,7,8}; Pointer definition: int *p; p= &a[0]; Int * p; p= a; //p still points to the first element of the array q= &a[2]; // q points to a[2] wants to express the connection between p and q, which both point to elements in the same array. Pointer and integer addition and subtraction let p be a pointer to an element in the array of integers, and I an integer, then p+ I means that the pointer slides backward by I integers, and p-i means that the pointer slides forward by I integersCopy the code
-
Pointers and one-dimensional arrays:
- An array of name:
The one-dimensional array name is a pointer constant that holds the address of the first element of the one-dimensional array
- The relationship between indices and Pointers:
If p is a pointer variable, then p[I] is always equivalent to *(p+ I).
- Pointer variable operations:
Pointer variables cannot be added, multiplied, or divided; Two pointer variables can be subtracted only if they refer to different storage units in the same contiguous space
- How many bytes does a pointer variable take:
Sizeof (data type) Function: Returns the number of bytes of the data type. For example, sizeof(int) = 4, sizeof(char) = 1, sizeof(double) = 8. Sizeof (variable name) Function: Returns the number of bytes of the variable
How many bytes does a pointer variable, no matter what variable it points to, take, the pointer variable itself only takes four bytes and the address of a variable is represented by the address of the first byte of the variable, right
- Pointers and two-dimensional arrays
- Pointers and functions
*
Pointers and structures- Multilevel pointer:
int i = 10;
int *p = &i;
int **q = &p;
int k = &q;
则k = i
Topic: Dynamic memory allocation
1. Disadvantages of traditional arrays (static arrays) :
- The array length must be specified in advance and must be a constant integer, not a variable for example: int a[5]; //ok int len = 5; int a[len]; //error
- An array defined in a traditional form whose memory cannot be freed manually by the programmer. During the execution of a function, the space allocated for the array in the function persists until the function is finished
- The length of an array, once defined, cannot be changed and cannot be dynamically expanded or shrunk as the function runs
- The array defined by function A can be used by other functions while function A is running. However, after function A is running, the array defined by function A cannot be used by other functions
Code examples:
#include <stdio.h>
void g(int * pArr, int len)
{
pArr[2] = 88;
}
void f(void)
{
int a[5] = {1.2.3.4.5}; //20 bytes of storage space cannot be released manually by the programmer, it can only be released automatically by the system when this function is finished
g(a, 5);
printf("%d\n", a[2]);
}
int main(void)
{
f();
return 0;
}
Copy the code
2. Why allocate memory dynamically: Dynamic arrays solve the four disadvantages of traditional arrays (static arrays)
- Use of the malloc function
#include <stdio.h>
#include <malloc.h> // Malloc is an abbreviation for memory allocate
int main(void)
{
int i = 5; // 4 bytes allocated (statically allocated)
int*p = (int *)malloc(4);// Dynamic allocation
*p = 5; //*p represents a variable of type int, but the memory allocation of *p is different from that of int I = 5
free(p); // Free (P) means to free the content pointed to by P, but the memory of the P variable itself is statically allocated and cannot be freed manually by the programmer
printf("Release \ n");
return 0;
}
/*1. To use the malloc function, you must add the malloc.h header. Malloc returns only the address of the first byte. 5. Int *p A line of 8 bytes is allocated, including 4 bytes for the p variable and 4 bytes for the memory pointed to by p. What p points to is dynamically allocated */
Copy the code
- Use of the malloc function 2:
#include <stdio.h>
#include <malloc.h>
void f(int *q)
{
*q = 100;
free(q); // Free the memory pointed to by q. This sentence must be commented out otherwise it will cause an error on line 17
}
int main()
{
int *p = (int *)malloc(sizeof(int)); //sizeof (int) returns the number of bytes int takes up
*p = 10;
printf("%d\n", *p);
f(p); //p is an int *
printf("%d\n", *p);
return 0;
}
Copy the code
3. Dynamic memory allocation example _ dynamic one-dimensional array construction:
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int a[5]; // If int is 4 bytes, the array contains 20 bytes in total, and each 4 bytes is used as an int variable
int len;
int *pArr;
int i;
// Dynamically construct a one-dimensional array:
printf(Please enter the number of elements you want to store:);
scanf ("%d", &len);
pArr = (int *)malloc(4 * len); // This line dynamically constructs a one-dimensional array of length len, array name pArr, each element of type int similar to int pArr[len]
// Assign to a dynamic one-dimensional array
for (i = 0; i < len; ++i)
scanf("%d", &pArr[i]);
// Output a one-dimensional array:
printf("The contents of a one-dimensional array are: \n");
for (i = 0; i < len; ++i)
printf("%d\n", pArr[i]);
//free(pArr); // This line can be used to manually release the dynamically allocated array
return 0;
}
Copy the code
Running results: 4. Static memory vs. dynamic memory:
Static memory is automatically allocated and released by the system
And static memory is inThe stackThe distribution of
While dynamic memory is allocated manually by the programmer, manually freed dynamic memory is allocated in the heap. Problems with using memory across functions: Static memory cannot be used across functions
#include <stdio.h>
void f(int **q) //q is a pointer variable, no matter what type of pointer variable q is, it only takes 4 bytes
{
int i = 5;
*q = &i; // equivalent to p = &i;
}
int main(void)
{
int *p;
f(&p);
printf("%d\n", *p); // This sentence is grammatically correct, but logically incorrect
return 0;
}
Copy the code
Running results: Dynamic memory can be used across functions
#include <stdio.h>
#include <malloc.h>
void f(int **q)
{
int i = 5;
*q = (int *)malloc(sizeof(int)); //sizeof(data type) returns the number of bytes for that data type
**q = 5; P = (int *) malloc(sizeof(int))
}
int main(void)
{
int *p;
f(&p);
printf("%d\n", *p);
return 0;
}
Copy the code
Running results:
The code field
A pointer causes a function to return more than one value:
#include <stdio.h>
void g(int *p, int *q)
{
*p = 1;
*q = 2;
}
int main(void)
{
int a = 3, b = 5;
g(&a, &b);
printf("%d %d\n", a, b);
return 0;
}
Copy the code
The results:
Determining a one-dimensional array takes several parameters:
#include <stdio.h>
void f(int * pArr, int len)
{
pArr[3] = 88;
}
int main(void)
{
int a[6] = {1.2.3.4.5.6};
printf("%d\n", a[3]); // pArr[3] in line 5 is the same variable as a[3] in lines 12 and 14
f(a, 6);
return 0;
}
Copy the code
The results:
#include <stdio.h>
void f(int *pArr, int len) // The f function can print the contents of any one-dimensional array
{
int i;
for (i = 0; i < len; i++)
printf("%d ", *(pArr + i));
printf("\n");
}
int main(void)
{
int a[5] = {1.2.3.4.5};
int b[6] = {-1, -2, -3, -4, -5, -6};
int c[100] = {1.99.22.33};
f(a, 5); //a is an int *
f(b, 6);
f(c, 100);
return 0;
}
Copy the code
The results: Conclusion:Determining a one-dimensional array takes two arguments:
The address of the first element of the array; Array length
Exercises after class:
1. The output result is shown as follows:
2.
Ref has four elements
3.
The address of ref is 0X61FE00
Ref is the name of the array, pointing to the first element of the array, and ref + 1 means that the pointer slides back an integer to point to the second element
What does ++ref mean, because ref is a constant and not a variable
4.
A. * PTR is 12 and * (PTR + 2) is 16
B. * PTR is 12 and * (PTR + 2) is 14
5.
A. The value of ** PTR is 12, and the value of ** PTR + 1 is 16
B. ** PTR is 12, and ** (PTR + 1) is 14