当前位置:网站首页>C language review (pointer)
C language review (pointer)
2022-07-28 07:12:00 【The most beautiful wish must be the craziest】
The pointer
A pointer is an address . It points to ( representative ) A piece of space in memory
The type of pointer
1、 The type of pointer
Remove the pointer name from the pointer declaration statement , The rest is the type of the pointer
2、 The type that the pointer points to
The pointer name in the pointer declaration statement and the pointer declarator to the left of the name * Get rid of , The rest is the type the pointer points to .
(1)int*ptr; // The pointer type is int* The type the pointer points to is int
(2)int**ptr; // The pointer type is int** The type the pointer points to is int* The content pointed to is also a pointer a, This pointer a The type is int* The content pointed to is int( there a It's a random name , Convenient description )
(3)int(*ptr)[3]; // The type the pointer points to is int()[3]
(4)int*(*ptr)[4]; // The type the pointer points to is int*()[4] The content pointed to is also a pointer a, This pointer a The type is int*()[4],a Type of point int()[4], Yes, there is 4 Array of elements , The contents of the array are int type ( there a It's a random name , Convenient description )
The operation of the pointer
The meaning of the pointer's self increment and self subtraction operation is different from that of the usual numerical addition and subtraction operation , The result is related to the type pointed to by the pointer .
char a[20];
int *ptr=(int *)a; // Cast does not change a The type of
ptr++;
The type the pointer points to is int,int The length of is 4.ptr++, The compiler added sizeof(int), therefore ptr At this time, the value is added 4, Because array a yes char type , The length of each element is 1, And the address is continuous , therefore ptr At this point, the content is a[4]
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
hypothesis p The value of is 0x100000. What are the values of the expressions in the following table ?
p + 0x1 = 0x___ ?
(int)p + 0x1 = 0x___?
(int*)p + 0x1 = 0x___?
Adding and subtracting a pointer variable from an integer does not directly add or subtract the integer with the address in the pointer variable . The unit of this integer is not byte But the number of elements . therefore :p + 0x1 The value of is 0x100000+sizof(Test)*0x1. The size of this structure is 20byte. therefore p +0x1 The value of is :0x100014.
(int)p + 0x1 The value of ? This involves coercion , Put the pointer variable p The saved value is forced into the number of shapes . Once any value is cast , Its type changes . So this expression is actually an integer plus another integer . So the value is :0x100001.
( int*)p + 0x1 The value of ? there p Is cast to a pointer to an integer . So the value is :0x100000+sizof(int)*0x1, be equal to 0x100004.
Arrays and pointers
1、 Array name
The array name is equivalent to a pointer , Point to the first address of the array
2、 Pointer array and array pointer
Pointer array , The contents stored in the array are pointers . Array pointer , It's a pointer to an array .
int *p1[3]; // Pointer array brackets [] High priority , Therefore, p1 First combine , Represents an array , This array has 3 Elements , this 3 Each element is a pointer , They point to int Type data . You can think of it this way : If there is such a definition int p[3], It's easy to understand that this is a place with 3 individual int An array of elements of type , Then put the int Switch to int*, It means that the element type in the array is int* type ( Point to int Pointer to type data ).
int (*p2)[3]; // Array pointer Parentheses make p2 And * combination , Express p2 It's a pointer , This pointer points to an array , Array has 3 Elements , The type of each element is int type . You can think of it this way : If there is such a definition int p[3], It's easy to understand that this is a place with 3 individual int An array of elements of type , So the array name p Change it to *p2, That is to say p2 It's a pointer , Points to this array .
int a = 1, b = 2, c = 3;
int *p1[3];
p1[0] = &a;
p1[1] = &b;
p1[2] = &c;
// If p1++ , be p1 Just added sizeof(int), Because it points to int type . It can be understood in this way , brackets [] High priority ,p1 With the first [] combination , And again * combination ,p1 And [3] See it as a whole after combination a So it became int *a, So the type of point is int
int a[3] = {1, 2, 3};
int (*p2)[3] = a;
printf("0x%x \n", p2++);
// If p2++, be p2 Added 12, Because the type it points to is int ()[3], Is an array of three elements , Its length is 12
a And &a
Actually a and &a The results are the first address of the array , But their types are different .a Express &a[0], That is, take the address of the first element of the array ,a+1 Indicates the first address +sizeof( Element type ).&a Although the value is the address of the first element of the array , But the type is : type (*)[ Number of array elements ], therefore &a+1 The size is : The first address +sizeof(a).
You should know that the array name is the first address of the array at the same time , Also know , The array name is just “ amount to ” The pointer , Not really a pointer , The array name is just a constant ( A constant whose value is the address of the first element of the array ), So it can't be ++ perhaps – operation . And constants can't get addresses , And the reason why &a, In fact, the a The meaning of is not the original array name , It now represents the entire array .
Structure and pointer
1、 Need to use -> Arrow operator to get the member variables in the structure
2、 The structure name is not an address , It needs to be addressed (&) operation .
Functions and pointers
A function pointer , Its essence is a pointer variable , This pointer points to this function . In conclusion , A function pointer is a pointer to a function . The name of the function represents the address of the function .
/* Declaration format Type specifier (* Function name ) ( Parameters ) */
int (*fun)(int x,int y);
/* Assign the address of a function to the function pointer Either way is OK */
fun = &Function;
fun = Function;
/* Function pointer call Both of the following can be , Suggest the first , Be clear */
x = (*fun)();
x = fun();
Function pointer array
int (*p[4])(int, int);// Let's start with the middle part , identifier p And brackets [] combination ( High priority ), therefore p Is an array , Array has 4 Elements ; Then the rest represents a function pointer , So the element type in the array is a function pointer , That's the address of the other functions
/* Sample code */
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }
int main()
{
int a = 4, b = 2;
int (*p[4])(int, int);
p[0] = add;
p[1] = sub;
p[2] = mul;
p[3] = divide;
printf("%d + %d = %d \n", a, b, p[0](a, b));
printf("%d - %d = %d \n", a, b, p[1](a, b));
printf("%d * %d = %d \n", a, b, p[2](a, b));
printf("%d / %d = %d \n", a, b, p[3](a, b));
}
Pointer function , In a nutshell , It's a function that returns a pointer , Its essence is a function , The return value of this function is a pointer .
/* Declaration format Type identifier * Function name ( Parameter table )*/
int *fun(int x,int y);// The return value is an address
边栏推荐
- shell---函数
- Understanding of C language EOF
- MOOC Weng Kai C language week 7: array operation: 1. array operation 2. Search 3. preliminary sorting
- Tutorial on regularization
- Esxi community network card driver updated in March 2022
- 根据excel生成create建表SQL语句
- NAT-网络地址转换
- Uni app double click event simulation
- 登录进oracle10g的oem,想管理监听程序却总是弹出帐号密码输入页面
- Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0
猜你喜欢

Easypoi export table with echars chart

VNC Timed out waiting for a response from the computer

Neo4j running error occurred during initialization of VM incompatible minimum and maximum heap sizes spec

MySQL build database Series (I) -- download MySQL

DHCP服务

freemarker合并单元格,if、else标签的使用,null、空字符串处理

easypoi一对多,合并单元格,并且根据内容自适应行高

Esxi community nvme driver update v1.1

Static and floating routes

Bond mode configuration
随机推荐
VNC Timed out waiting for a response from the computer
Uni app double click event simulation
Media set up live broadcast server
Implementation method of Bert
MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation
C language push box
Icc2 analysis timing artifact analyze_ design_ violations
Easypoi export interlaced style settings
Standard C language summary 1
Shell--- circular statement practice
DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
Forward and backward slash notes
Canvas drawing 2
Wangeditor (@4.7.15) - lightweight rich text editor
Standard C language learning summary 7
DNS domain name resolution
shell---sed语句练习
Results fill in the blank shopping list (teach you to solve it with Excel)
Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
MOOC Weng Kai C language fourth week: further judgment and circulation: 1. Logical types and operations 2. Judgment of cascading and nesting