当前位置:网站首页>9. Pointer (upper)
9. Pointer (upper)
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
5. The operation of the pointer
The operational relationship of pointers
1. What is the pointer ?
The pointer ? What is a pointer ? fundamentally , The pointer (pointer) Is a variable whose value is the memory address ( Or data objects ). just as char The value of a type variable is a character ,int The value of a type variable is an integer , The value of the pointer variable is the address . Pointer in spoken language usually refers to pointer variable , The variable used to hold the address .
int main()
{
int num = 10;
int* p = #
printf("%p\n", p);
return 0;
}
- In the above code ,& To get the address symbol , Put the variable num Take out the address of and assign it to p.
- * Indicates that the declared variable is a pointer , and int Indicates that the type of pointer is integer ;int*p It means ,p It's a pointer ,*p yes int type .
- utilize printf When the function prints the address ,%p The corresponding address is .
With X86 Run the above code in the environment , give the result as follows :
009BFA40 In order to 16 In base notation , Variable num It is stored in this address .
Just like the address in our life ,C The address in the language is also unique .
2. The size of the pointer
Since the pointer is used as a variable to store the address , Then the computer will certainly open up a space for storing pointers .
Take the above code as an example , adopt sizeof You can calculate the size of the pointer :
int main()
{
int num = 10;
int* p = #
printf("%d\n", sizeof(p));
return 0;
}
stay X86 In the environment , namely 32 In the machine : Pointer 4 Bytes .
stay X64 In the environment , namely 64 In the machine : Pointer 8 Bytes .
1 Bytes are equal to 8 A bit , that 32 In the machine , The pointer can indicate 2^32 An address ;64 In a bit machine , Can represent 2^64 An address .
The smallest unit in the pointer is a byte , This is after careful thinking and weighing . That is, we can query a char Address of type character , But you can't query the address of a bit in this character .
3. The type of pointer
When creating a variable , We need to declare the type for it , for example : Use integer to express age ,float Type indicates height 、 Weight etc. ; The same is true when creating pointers , For different types of variables , Declare a pointer of the corresponding type to store its address .
char *pc = NULL;
int *pi = NULL;
short *ps = NULL;
long *pl = NULL;
float *pf = NULL;
double *pd = NULL;
(* And pointer name , Not affecting use )
Pointer dereference (*)
Through the indirect operator *(indirection operator) You can use the address to change the variable , This operation is also called “ Quoting ”.
#include<stdio.h>
int main()
{
int num = 10;
printf(" Before change :%d\n", num);
int* p = #
*p = 2;
printf(" After change :%d\n", num);
return 0;
}
We have not changed directly num Value , It's about finding num Storage address , Through dereference operation, it indirectly changes num Value .
The pointer +- Integers
#include<stdio.h>
int main()
{
int num = 10;
int* pi = #
char* pc = (char*)#
printf("%p\n", &num);
printf("%p\n", pi);
printf("%p\n", pi + 1);
printf("%p\n", pc);
printf("%p\n", pc + 1);
return 0;
}
expression char* pc = (char*)&num: Will integer variable num Take out your address , Cast to char* type , Then give to char* Pointer variable of type pc.
Found by test : The pointer type corresponds to the pointer +- Time span , namely char Pointer to type +1 Visit later 1 Bytes ,int Pointer to type +1 Visit later 4 Bytes .
4. Wild pointer
Concept : The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )
The cause of the wild pointer
Pointer not initialized
#include <stdio.h>
int main()
{
int* p;
*p = 10;
return 0;
}
Local variable pointer not initialized , The default value is random .
Pointer cross boundary access
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
int i = 0;
for (i = 0; i < 11; i++)
{
*(p++) = i;
}
return 0;
}
Array arr The subscript of is 0~9, When the pointer points to an array arr The scope of time ,p It's a wild pointer .
The space that the pointer points to is released
When the variable pointed to by the pointer is released , The pointer is a wild pointer .
Avoid wild pointer
1. Pointer initialization
2. Watch out for the pointer
3. Pointer to space release even if set to NULL
4. Avoid returning the address of a local variable
5. Check the validity of the pointer before using it (avoid)
#include <stdio.h>
#include <assert.h>
int main()
{
int* p = NULL;
int a=10;
int* p = &a;
assert(p != NULL);
*p = 10;
if(p!=NULL)
*p = 20;
return 0;
}
5. The operation of the pointer
The operational relationship of pointers
#include<stdio.h>
#define N 5
int main()
{
int ch[N] = { 0 };
int* vp;
for (vp = &ch[0]; vp < &ch[N];)
{
*vp++ = 1;
}
return 0;
}
- expression vp = &ch[0] Indicates that the address of the first element in the array is assigned to vp.
- vp < &ch[N] It means that you will vp The address stored in the array is compared with the address of the next element of the array , as long as vp Less than it , It means that it is still accessed in the array , No boundaries .
- *vp++ It's actually *(vp++), One integer per backward access .
The pointer - The pointer
#include<stdio.h>
int my_strlen(char* s)
{
char* p = s;
while (*p != '\0')
p++;
return p - s;
}
int main()
{
char ch[]="hello";
printf("%d\n", my_strlen(ch));
return 0;
}
The standard stipulates
Allows a pointer to an array element to be compared with a pointer to the memory location after the last element of the array , However, it is not allowed to compare with a pointer to the memory location before the first element .
6. Pointers and arrays
This part is dedicated to a chapter , Please have a look at Array 、 The relationship between pointer and array .
7. The secondary pointer
Pointer variables are also variables , If it is a variable, it must have a storage address . Then the pointer that stores the address of the pointer variable , It is called secondary pointer .
#include<stdio.h>
int main()
{
int a = 10;
int* p1 = &a;
int** p2 = &p1;
**p2 = 20;
return 0;
}
8. Pointer array
Pointer arrays are arrays , It's just an array for storing pointers .
#include<stdio.h>
int main()
{
int arr1[] = {1,2,3};
int arr2[] = {4,5,6};
int arr3[] = {7,8,9};
int* p1 = arr1;
int* p2 = arr2;
int* p3 = arr3;
int* p[] = { p1,p2,p3 };
return 0;
}
int* p[] = { p1,p2,p3 } Indicates that the type of data stored in the array is int* type , The stored data are p1,p2,p3.
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", *(*(p + i))+j);
}
putchar('\n');
}
Set up two for Loop print pointer array p, It can be found that one-dimensional pointer array realizes the effect of two-dimensional array .
边栏推荐
- Quickly generate illustrations
- TYUT太原理工大学2022数据库题库选择题总结
- Change vs theme and set background picture
- Smart classroom solution and mobile teaching concept description
- 5. Download and use of MSDN
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
- One article to get UDP and TCP high-frequency interview questions!
- TYUT太原理工大学2022数据库考试题型大纲
- String类
- 3.输入和输出函数(printf、scanf、getchar和putchar)
猜你喜欢
魏牌:产品叫好声一片,但为何销量还是受挫
TYUT太原理工大学2022数据库大题之概念模型设计
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
西安电子科技大学22学年上学期《信号与系统》试题及答案
13 power map
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
System design learning (I) design pastebin com (or Bit.ly)
西安电子科技大学22学年上学期《基础实验》试题及答案
Quickly generate illustrations
随机推荐
5. Download and use of MSDN
Common method signatures and meanings of Iterable, collection and list
1.C语言初阶练习题(1)
Experience summary of autumn recruitment of state-owned enterprises
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
Interview Essentials: talk about the various implementations of distributed locks!
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Inheritance and polymorphism (I)
初识C语言(上)
2.初识C语言(2)
String class
Comparison between FileInputStream and bufferedinputstream
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
最新坦克大战2022-全程开发笔记-1
FileInputStream和BufferedInputStream的比较
Relational algebra of tyut Taiyuan University of technology 2022 database
Wei Pai: the product is applauded, but why is the sales volume still frustrated
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
TYUT太原理工大学2022数据库之关系代数小题
分支语句和循环语句