C language learning - pointers

1. Pointers and pointer variables and definitions, use
1) What is a pointer:
pointer is an address ; (for example: int *p,a; p=&a, it can be said that p points to variable a, or p is a pointer to variable a)
2) What is a pointer variable:
pointer variables are used to storage address . (The meaning of defining the pointer is to access the memory unit through the pointer. At the same time, because the array or function is stored continuously, the first address of the array or function can be obtained by accessing the pointer variable through the pointer. In C language, a data type or Structures tend to occupy contiguous memory units)
3) Supplement:
One byte occupies one address, two bytes occupy two, and so on.
4) Example:
int *p,z; (the definition of a pointer variable, which can be Define multiple at the same time , multiple spaces can be added between * and the variable name, the pointer variable itself has no data type, and its data type here is the data type pointing to the memory unit it refers to )
p=&z; (The assignment of pointer variables can also be done like this: int *p=&a, or two pointers of the same type can be assigned to each other, such as: int *p, *z, a; p=&a; z=p
*p=4;
printf("%d", *p ) {If instead of *p, use p, the output is the address }
and: The output of the character pointer that appears later to the content is the direct output of the pointer variable name, that is, the pointer variable name is directly behind
5) Notes on pointer assignment:
In principle, a pointer can only point to variables of the same type. If the type of the pointer on the right is different from the one on the left when assigning a value to the pointer, then the coercion , converts the right to the left
In the assignment statement, there cannot be * before the assigned pointer variable, that is: the pointer variable p is defined in advance, It is an error to use *p=&a in an assignment statement.
Under Standard C, you cannot use an auto variable to assign a value to a static pointer.
6) Zero pointer and null type pointer
a. int *p=NULL means the same as int *p=0 zero pointer
b. A null type pointer means that you don’t know what type the pointer points to, or it can be any type, so you need to cast it when you use it
7) A reference to a pointer variable:
* is an indirect reference operator, a unary operator, with the same priority as ++, --, and has right associativity, and it should be noted that a++ and (*p)++ mean the same, *p++ and * (p++ ) means the same as address++
8) Note:
Pointer variables need to be defined first and then assigned, and can only be used after assignment. Use without assignment is a wild pointer, which can easily lead to system crashes.
Pointer variables are also variables and also need to occupy memory units, but different types of The memory unit occupied by the pointer variable is the same, depending on the compilation environment.


2. Pointer and address operations:
1) Addition and subtraction of pointer variables:
A pointer is an address, an unsigned integer, It cannot participate in multiplication and division like integers , and the addition and subtraction of pointers is not a simple addition and subtraction, but: p+(-)n= ADDR +(-) n*sizeof*(data type) {ADDR is the current p's address}
The addition of two pointers does not make any sense, but the subtraction can indicate the number of memory cells or the number of elements that differ between the two pointers

3. Pointers and arrays
1) Array pointer: The pointer to the array is actually the starting address of the array in memory (the name of the array variable).
2) Reference: int a[10];
p=a or &a[0]
3) Note: a+k is equivalent to p+k, &a[k], which is the address of a[k]
a[k]=*(a+k)=*(p+k)= p[k]
p+1 points to the next element of the array instead of simply +1 the value of the pointer variable p, the actual change is: p+1*sizeof(data type)


4) Use pointers to access multidimensional arrays
a. Use general pointer variables to access multi-dimensional arrays (the storage order of array data units in memory is row-wise)
Citation format: *(p+i*)
The triangle marks the point!
b. Definition of row pointer:
datatype (*row pointer variable name) [const expression]
illustrate:
The row pointer variable name and * must be enclosed in parentheses;
The constant expression specifies the size of the second dimension of the two-dimensional array, which is not negligible;
The data type symbol represents the element type of the one-dimensional array pointed to by the row pointer
c. Refer to the element form of the two-dimensional array a through the row pointer:
p [i][j]; *(p[i]+j) ; *((p+i)+j); (*(p+i))[j]
d. The general form of assignment to a two-dimensional array row pointer variable is:
Two-dimensional array name + integer constant n For example: p=a+1;
& two-dimensional array name [integer constant], for example: p=&a[0]

5) An array of pointers (an array of pointers, which can also be said to be an array of storage addresses)
a. Reference format: data type * variable name [constant expression]
E.g: char a[3]; char *p[3]; p[0]=&a[0]; p[1]=&a[1]; p[2]=&a[2];
b. Comparison of pointer arrays and row pointer variables of two-dimensional arrays

6) Pointers and Strings
a. Definition: The string itself is a character array terminated with '\0', and the address of the string in memory (ie the first address) is called a character
string pointer
b. Definition and assignment in the program :
char * character pointer variable name = string constant
char * character pointer variable name;
char pointer variable name = string constant
E.g:
char *pstr=“I am a big man”
or char *pstr; pstr="I am a big man"
Note: The above result is to assign the first address of the string to the string pointer variable
c. String reference
Quoting one by one:
Quoting in its entirety:
The system first outputs pstr to point to the first character, and then automatically adds 1 to pstr to make it point to the next character until the end of the string is encountered.

d. Comparison between character pointer variable and character array
1) The storage content is different
The character pointer variable stores the address, while the array stores the string itself
2) The assignment method is different
Character pointer variables can be directly assigned to the pointer variable name, and the variable name of the array is an address, which is a constant, so it cannot be directly assigned
例如:char *pointer ,pointer="This is an example"
char carray[20]="This is an example."

e. Note on the use of character pointer variables:
When a character pointer points to a string, it is no different from a character array containing a string except that it can be assigned a value. Secondly, if the character pointer variable does not point to a character array or other valid memory, that is, it cannot be used without assignment, otherwise error



7) Pointers and dynamic memory allocation
a . Static memory allocation
After the variable or array is defined in the program, the system will allocate the corresponding memory unit to the variable or array according to its data type and size.

b. Dynamic memory allocation
Dynamic memory allocation refers to the allocation of a continuous memory unit of suitable size according to the actual needs of the program during the running process of the program. The allocation of dynamic memory requires a pointer variable to record the starting address of the memory.

c.malloc() function
The function malloc() is used to allocate several bytes of memory space, and returns a pointer to the address of the memory area in this area. If the system cannot provide enough memory units, the function will return a null pointer NULL, the function prototype:
void *malloc(unsigned int size){size is the allocated memory size, in bytes}
Note: If the allocation is unsuccessful using this function, the return value is a null null pointer
If successful, the return value is the first address of any type of memory block
Notes on the use of malloc:
(1)malloc前面必须加上一个指针类型转换符,因为malloc函数返回的是个任意类型的指针,它一般与左边的指针类型一致
(2)malloc所带的参数是内存字节数,一般形式为:分配数量*sizeof(内存单元类型)
(3)malloc可能返回一个NULL,所以一般调用该函数,其后面都要接一个判断语句,判断是否分配成功

d.calloc()函数
该函数用于非 若干个同一类型的数据分配连续存储空间,其中每个数据项的长度单位为字节,通过调用该函数所分配的存储单元,系统将其自动置为初值0,函数原型:
void *calloc(unsigned num,unsigned size)
(num:表示向系统申请的 内存空间的数量,size:表示申请的 每个内存空间的字节数)
例如:pscore=(int*)calloc(n,sizeof(int))
上述相当于:向系统申请了 一个一维数组,n为数组大小,sizeof(int)为数组元素数据类型
注意:调用这个函数一样需要在其后面判断是否分配成功

e.realloc()函数
该函数用于改变原来分配的内存空间大小,函数原型:
void *realloc(void *p,unsigned int size)
功能是将指针p所指向的存储空间的大小改为size个字节,如果分配内存失败,就返回NULL,如果成功,将返回存储空间的首地址,该首地址与原来分配的首地址 不一定相同


f.动态内存释放
在用完动态内存之后就要释放内存,一定要坚持:“好借好还,再借不难”
格式:void free(void *block)
一般malloc calloc realloc都要和free函数成对出现
且他们的头文件为stdlib.h或者malloc.h或者alloc.h

8)多级指针
a.二级指针的定义与引用
[存储类型] 数据类型 **变量名
定义格式说明:
存储类型是指二级指针变量本身的存储类型
数据类型符可以是任何一种有效的数据类型表示符,是指针变量 所指向的最终目标变量的数据类型
*号的个数表示指针的级数
例如:int a=3
int *p1;
int **p2;
p1=&a;
p2=&p1;
**p2=3;

9)指针作为函数参数
a.引入:参数的几种传递方式:
(1)若传递的是值,则被调函数的执行不会影响调用函数的实参
(2)若传递的是地址,那么在被调用函数中就可以对该地址所对应的内存单元进行引用,既可以读取该内存单元的值,也可以对该内存单元的值进行修改, 要实现地址调用,就必须用到指针型参数



10)指针作为函数返回值——指针函数
a.定义格式:
函数类型 *函数名 ([形参1,形参2,形参3]...)
b.注意:如果一个函数返回一个指针,请注意, 不能返回auto型局部变量的地址,但可以返回static型变量地址,因为局部变量用完就消失
需要把握原则:返回的指针所对应的内存空间不能因为该指针函数的返回而被释放掉
c.返回的指针通常有:
函数中 动态分配的内存的首地址
函数中的 静态变量或全局变量所对应的存储单元的首地址
通过指针形参所获得的实参的有效地址

10)指向函数的指针——函数指针
























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325805378&siteId=291194637