Pointer
The pointer and pointer variables are the address, the address is the pointer address is the memory unitnumberingA pointer variable is a variable pointer and pointer variable that holds the address (the number of memory cells) is two different concepts but note: Usually we simply call pointer variables as pointers, and the actual meaning of pointers is an action-constrained nonnegative integer importance: Representing some complex data structures fast transfer of data reduces Memory consumption "Focus" enables the function to return more than one value "focus" direct access to the hardware can easily handle strings is the basic definition for understanding references in object-oriented languages: Address: number of memory units
non-negative integer starting at 0Range: 4G "0---4g-1" Category: 1. Basic type pointer "Focus" NOTE: * Meaning of
1. Multiplication 2. Define pointer variables int &N bsp;*p;//defines a variable named p, int * indicates that p can only hold int variable address 3. Pointer operators &N Bsp The operator is placed in front of the defined pointer variable If p is an already defined pointer variable *P indicates how a variable with the content of P can modify the value of the normal variable of the keynote function through the modulated function 1. The argument must be the address of the normal variable 2. Parameter must be pointer variable &N Bsp 3. Pass * parameter name = in the modulated function You can modify the value of the underlying variable of the keynote function 2. Pointers and arrays pointers and one-dimensional arrays One-dimensional array names: One-dimensional array name is a pointer constant It holds the address of the first element of the array Subscript and pointer relationships If p is a pointer variable, then & nbsp P[i] is always equivalent to * (p+i) to determine a one-dimensional array requires several parameters "ifA function to accept a one-dimensional array, what conditions are required "the address and array length of the first element of a set
# include <stdio.h>// The F function can output the contents of any one-dimensional array void F (int * Parr,int Len) { int I; for (i = 0 ; i < Len;++i) printf ( " %d Span style= "COLOR: #800000" > ", * (PARR + i)); /* (PARR + i) equivalent to Parr[i] is also equivalent to the array [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 * type that requires the address and length F (b,6) of the first element of the array; f (c,100); return 0; }
# include <stdio.h>//the F function can output the contents of any one-dimensional arrayvoidFint* PARR,intLen) {parr[3] = the;}intMainvoid){ inta[6] = {1,2,3,4,5,6}; printf ("%d\n", a[3]); F (A,6); printf ("%d\n", a[3]); return 0; }/*488*/Pointer variables cannot be added, multiplied, or divided, and can only be subtracted if the two pointer variable points to a different storage unit in the same contiguous space, then the two pointer variables can be subtracted
# include <stdio.h>intMainvoid){ inti =5; intj =Ten; int*p = &i; int*q = &J; inta[5]; P= &a[1]; Q= &a[4]; printf ("p and Q point to units separated by%d units \ n", Q-p); //p-q no practical significance return 0; }A pointer variable exactly takes up a few bytes "non-focus" pre-knowledge: & nbsp sizeof (data type) &NBSP ; Function: The return value is the number of bytes in the data type &NBSP ; Example: sizeof (int) = 4 sizeof (char) = 1 &NBSP ; sizeof (double) = 8 & nbsp sizeof (variable name) Function: The return value is the number of bytes in the variable assumes p points to char type variable (1 bytes) &N Bsp Suppose P points to a char type variable (4 bytes) assumes p points to char type variable (8 bytes) p q The number of bytes that R itself occupiesIs it consistent? The answer: Consistent summary: 1. A pointer variable, regardless of the number of bytes it points to the ear variable The pointer variable itself accounts for only four bytes 2. One The address of a variable uses the address of the first byte of the variable to represent pointers and two-dimensional arrays 3. Pointers and Functions 4. Pointers and structures 5. Multi-level pointers
//Multi-level pointers# include <stdio.h>intMainvoid){ inti =Ten; int*p = &i; int**q = &p; intR = &Q; /**r = Q->**r = *q = P->***r = **q = *p = i*/ //r = &p; //because R is an int type, R can only hold the address of an int * * type variableprintf"i =%d\n",***R); return 0;}
//Multi-level pointers# include <stdio.h>voidFint**Q)//*q is P .{ } voidg () {inti =Ten; int*p = &i; F (&P);//p is an int * type, &p is an int * * Type}intMainvoid) {g (); return 0;}
Topics:
Dynamic memory allocation "key difficulties"
Disadvantages of traditional arrays:1. The length of the array must be specified beforehand and can only be a constant integer, not a variable example: int a[5]; OK int len = 5;int a[len];//error 2. An array of traditional form definitions, the memory programmer of the array cannot be released manually during a function run, the system allocates space for the array in the function to persist until the function When the operation is complete, the space of the array is freed by the system 3. The length of an array cannot be dynamically expanded or shrunk when the function is running. Once defined, the length of the array can no longer be changed. The array of 4.A function definitions may be used by other functions while the A function is running, but after the A function is finished, the A function Arrays can no longer be used by other functions in the traditional way defined arrays cannot be used across functionsWhy you need to allocate memory dynamicallyDynamic arrays are a good solution to the 4 drawbacks of traditional arrays, which are also called static arrays.Dynamic Memory Allocation example--construction of dynamic array
/*malloc is the abbreviation for memory (RAM) allocate (allocation)*/# include<stdio.h># include<malloc.h>//cannot saveintMainvoid){ inti =5;//4 bytes Allocated static allocation int*p = (int*)malloc(4);//12 Rows /*1. To use the malloc function, you must add malloc.h this header file 2. The MALLO0C function has only one formal parameter, and the formal parameter is integer 3.4 to indicate that the request system allocates 4 bytes 4.malloc function for this program Only the first byte of address 5 can be returned. 12 rows are allocated 8 bytes, the p variable is 4 bytes, the memory p points to 4 bytes of 6.P of memory is statically allocated, p points to the memory is dynamically allocated*/*p =5;//*p represents an int variable,//just *p the memory allocation of this integer variable is different from 11 rows Free(p);//Free (p) to release the memory that P occupies//the memory of P itself is static and cannot be manually released by the programmerprintf"Good comrades! \ n"); return 0; }
# include <stdio.h># include<malloc.h>intMainvoid){ intLen; int*PARR; inti; //Dynamic construction of one-dimensional arraysprintf"Please enter the number of elements you want to store:"); scanf ("%d",&Len); PARR= (int*)malloc(4*Len); //working with one-dimensional arrays for(i =0; I < len;++i) scanf ("%d",&Parr[i]); //output to one-dimensional arraysprintf"the contents of a one-dimensional array are: \ n"); for(i =0; I < len;++i) printf ("%d\n", Parr[i]); Free(PARR);//releasing the dynamically allocated array return 0;}Static memory and dynamic memory compared to static memory is automatically allocated by the system, automatically released by the system static memory is allocated in the stack of dynamic memory is manually allocated by the programmer, manual release of dynamic memory is allocated in the heap
the problem of using memory across functionsStatic variables cannot be used across functions
# include <stdio.h>voidFint**Q)//Q is also a pointer variable,//regardless of what type of pointer variable q is, it only accounts for 4 bytes{ inti =5; //*q equivalent to P Q and **q are not equivalent to P//*q = i; //error because *q = i is equivalent to P = i, which is wrong*q = &i;//p = &i; p = *q;-*q = &i;}intMainvoid){ int*p; F (&p); //printf ("%d\n", *p); //There is no problem with this statement syntax, but there is a problem with logic, it is statically assigned return 0;}
Dynamic memory can be used across functions
# include <stdio.h># include<malloc.h>voidFint**q) { *q = (INTN *)malloc(sizeof(int)); //sizeof (data type) The return value is the byte that the data type occupies**q =5;//*p = 5;}intMainvoid){ int*p; F (&p); printf ("%d\n", *p);//There is no error, the function is not terminated because it is dynamically allocated return 0;}
C Language Pointers