当前位置:网站首页>C pointer advanced 1-- > character pointer, array pointer, pointer and array parameter transfer, function pointer
C pointer advanced 1-- > character pointer, array pointer, pointer and array parameter transfer, function pointer
2022-06-29 23:20:00 【real Wangyanbin】
Advanced pointer
Character pointer
We are C The language pointer part learns that the pointer is equivalent to an address , A pointer variable is a variable that holds an address , The type of pointer variable determines the precision of pointer operation .± The size of operation pointer skipping is closely related to the type of pointer , So what type of pointer variable stores what type of address ( Is the data type of the data stored in the address ). What we call a pointer is a pointer variable .
As the name suggests, a character pointer is the address of the space where the character type is stored , Is a pointer to a character type . There are two main forms of addresses stored by character pointers ,
char ch = ‘W’;
char * pc = &ch;
// This situation is the most direct one —> The address where a character variable is storedchar *pc = “abcdef”;
// The first address where the string constant is stored , amount to —> Store string constant
When a character pointer stores a string constant , Is it similar to a character array :
- The character array name is equivalent to the address of the first element of the array , The character pointer variable name stores the first address of the string .
- Both character pointers and character array names can be used [] To traverse .
But they are different , There are two main points :
The array elements in the character array can be changed , But the string constant pointed to by the character pointer cannot be changed , Because a string constant is a constant , Constant values cannot be changed . But string constants stored in character pointers can be transformed .
int arr[] = “abcdef”
int *pc = “abcdef”;
arr[2]=‘F’;//YES
pc[2] = ‘F’;//ERROR
arr = “ABCDEF”;//ERROR
pc = “ABCDEF”;//YESA character pointer stores the address of a string constant , The same string constant stored by multiple different character pointers has the same address . Character arrays are used to store strings , The addresses of the first elements of different character arrays storing the same string are different
Look at an example :#include<stdio.h> int main() { const char* p1 = "abcdef"; const char* p2 = "abcdef"; char arr1[] = "abcdef"; char arr2[] = "abcdef"; if (p1 == p2) printf("p1==p2\n"); else printf("p1!=p2\n"); if (arr1 == arr2) printf("arr1 == arr2\n"); else printf("arr1 !=arr2\n"); return 0; }The final result of the above example :
p1==p2
arr1 != arr2
Array pointer
Array name and array pointer
We are C Language pointer part learned pointer array ( Details please see C Initial level of language pointer ), Pointer array is a pointer decorated array , Is an array of pointer types . Array pointers are arrays decorated with pointers , This data type is a pointer , Is the address of array type stored in pointer variable . What does the address of this array type mean , Let's recall the array name first .
The array name is the address of the first element of the array , But there are two special cases
- sizeof( Array name );
- & Array name
The above two array names represent the address of the entire array ,& Array name The extracted address is the address of the entire array , Array pointers are pointers to array types , So what the array pointer stores is & Array name
Format of array pointer : int (*pc)[10] ;
- //pc The first and combination , explain pc Is a pointer variable , Then the pointer points to a size of 10 An array of integers . therefore pc It's a pointer , Point to an array , It's called array pointer . The data type is int ()[10];
- Pay special attention to and :int *p[10]; Make a distinction ,p With the first [] combination , explain p Is an array , The element types in the array are int * .p The data type of is int *[10];
Application of array pointer
1. Traversing an array with an array pointer
#include<stdio.h>
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int(*p)[10] = &arr;
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
printf("%d ", *(*p + i));
}
return 0;
}
- p Is pointing to an array ,*p In fact, it is equivalent to the array name , The array name is the address of the first element of the array
therefore *p It is essentially the address of the first element of the array
But this kind of traversal looks complicated , It is more convenient to traverse directly with integer pointer and array name ?
The array pointer can be used in this way , But not the main use , Array pointers are mainly used to pass parameters to functions .
2. Receive a two-dimensional array with an array pointer as a formal parameter
#include<stdio.h>
void print(int(*p)[5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", p[i][j]);//*((p+i)+j)<==>p[i][j]
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
print(arr,3,5);
return 0;
}
2D array name arr Represents the address of the first element of a two-dimensional array , The first element of a two-dimensional array is the first row of a two-dimensional array, which is equivalent to the address of a one-dimensional array , So you can use an array pointer to receive .
Array pointer concept exercise
int arr[5];
int *parr1[10];
int (*parr2)[10];
int (*parr3[10])[5];
The first three lines can easily be seen as Array Pointer array Array pointer .
Mainly the fourth line ,parr3[10] You can see that it is an array Get rid of parr3[10] Got int (*)[5] Is the element type of the array . So the code means that there is 10 individual int (*)[5] Array of element types , It can also be said to be an array storing array pointers .
Array parameters 、 Pointer parameter
When writing code, it is inevitable to 【 Array 】 perhaps 【 The pointer 】 Pass to function , How to design the parameters of the function ?
One dimensional array parameters
#include <stdio.h>
void test(int arr[])//ok?
{
}
void test(int arr[10])//ok?
{
}
void test(int* arr)//ok?
{
}
void test2(int* arr[20])//ok?
{
}
void test2(int** arr)//ok?
{
}
int main()
{
int arr[10] = {
0 };
int* arr2[20] = {
0 };
test(arr);
test2(arr2);
}
The above function parameters are correct and can accept arguments smoothly .
Two dimensional array parameters
void test(int arr[3][5])//1.ok?
{
}
void test(int arr[][])//2.ok?
{
}
void test(int arr[][5])//3.ok?
{
}
void test(int* arr)//4.ok?
{
}
void test(int* arr[5])//5.ok?
{
}
void test(int(*arr)[5])//6.ok?
{
}
void test(int** arr)//7.ok?
{
}
int main()
{
int arr[3][5] = {
0 };
test(arr);
}
Two dimensional array parameters , Function parameter design can only omit the first [] The number of .
Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row . So it's easy to calculate .
So the function 1,3 The formal parameters are correct ,2 Formal parameter error
The two-dimensional array name represents the address of the first element of the two-dimensional array , That is, the address of the first row of the array , Equivalent to an array name , You should use an array pointer to receive ,4. Integer pointer error ,5. Pointer array error ,6 Array pointer is correct .
For the seventh parameter transfer problem , Some students think that the secondary pointer is the address of the address , It's not true , The two-dimensional array is the address where the first level pointer variable is stored , The two-dimensional array name is the address of the one-dimensional array , So it's wrong .
First level pointer parameter transfer
If the formal parameter of a function is a first-order pointer , Then the arguments that can be passed to the function are : Array name , First level pointer , Variable takes address , And secondary pointer dereference ( To use less );
void print(int* p)
{
}
int main()
{
int a = 10;
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int *pa = &a;
int** ppa = &pa;
print(pa);
print(arr);
print(&a);
print(*ppa);
return 0;
}
The secondary pointer transmits parameters
Generally, there are three cases when the second level pointer transfers parameters : The secondary pointer , The first level pointer takes the address , Pointer array name ;
Pointer array the array name represents the address of the first element , The first element is a pointer , So the pointer array name is the second level pointer
void test(int** p)
{
}
int main()
{
int a = 0;
int* pa = &a;
int** ppa = &pa;
int* parr[10];
test(ppa);
test(&pa);
test(parr);
return 0;
}
A function pointer
The previous array pointer , Secondary pointers are all pointing to various types of data , however C Language pointers don't just point to data , Can also point to a function . Functions also occupy memory space , The function name can be used as the first address of the space occupied by the function ,& The function name represents the first address of the space where the function is located .
#include<stdio.h>
void test()
{
printf("hello world");
}
int main()
{
printf("%p\n", test);
printf("%p\n", &test);
return 0;
}
An array pointer is a pointer to an array , The shaping pointer is a pointer to the shaping , It is inferred that the function pointer is a pointer to a function .
Function pointer form :void (*pfun1)();
Some of them are easily confused :void *pfun2(); () Is more cohesive than * strong , So what this code means is to declare a return value of void * Function of .
Two interesting codes
// Code 1
(*(void (*)())0)();
// Code 2
void (*signal(int , void(*)(int)))(int);
Code 1: It's a function call , Is to call 0 Function for address
- hold 0 Force type to void (*)()–> The parameterless return type is void The address of the function
- call 0 The function at the address
Code 2:
signal Is the function name , The above code is a function declaration .
Declarative signal The first parameter type of the function is int, The second parameter type is the function pointer , The function parameter pointed to by the function pointer is int, The return type is void,signal The return type of a function is also a function pointer , The function pointer points to the parameter of the function int, The return type is void.
Code 2 Too complicated , simplify :
typedef unsigned int uint;
typedef void(* pf_t)(int) ;// hold void(*)(int) The type is renamed to pf_t
边栏推荐
- 按头安利 好看又实用的点胶机 SolidWorks模型素材看这里
- sql刷题595. 大的国家
- math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
- label問題排查:打不開標注好的圖像
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Online text digit recognition list summation tool
- The development of grpc
- 基金的估值,费用,会计核算
- Detailed description of gaussdb (DWS) complex and diverse resource load management methods
- Free PDF to word software sharing, these software must know!
猜你喜欢

深入解析kubernetes controller-runtime

Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently

VS无法定位程序输入点于动态链接库

Online text digit recognition list summation tool

Processing of error b6267342 reported by AIX small machine in production environment

constexpr 函数

80-Redis详解

sql刷题595. 大的国家

How to solve the problem that the computer time is not automatically updated after proofreading

Cloud native enthusiast weekly: cool collection of grafana monitoring panels
随机推荐
分布式消息中间件设计
股票开户安全吗?上海股票开户。
写论文工具:LaTex在线网站
微博系统中”微博评论“的高性能高可用计算架构
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
Shell -- text processing command
Implementation principle of dynamic agent
基金的估值,费用,会计核算
Number theory - division and blocking
Sword finger offer 38 Arrangement of strings
[cooking record] - hot and sour cabbage
数据库-玩转数据-Pgsql 使用UUID做主键
Become the only key
PROJECT #1 - BUFFER POOL [CMU 15-445645]笔记
Static keyword continuation, inheritance, rewrite, polymorphism
111.简易聊天室14:聊天室客户端
NRM explanation
js函数相关的复习
收藏!这些提高程序员生产力的工具你用过吗?
Deep parsing of kubernetes controller runtime