当前位置:网站首页>C language - Advanced pointer
C language - Advanced pointer
2022-07-26 05:17:00 【Boring ~】
List of articles
One 、 Character pointer
The pointer is a variable , It's used to store the address , Variables that store an integer address are called integer pointers , A variable that stores the address of a character is called a character pointer .
If you assign a constant string to a character pointer , The character pointer stores the address of the character , Only one character address , So you can only put the address of the first character in it , You can't put the addresses of all characters in it , Just like a single bed can only be used by one person .
int main()
{
const char* pstr = “hello bit.”;
printf(“%s\n”, pstr);
return 0;
}
Put it in pstr The address in is just h The address of .
Interview questions :
#include <stdio.h>
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char *str3 = "hello bit.";
const char *str4 = "hello bit.";
if(str1 ==str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if(str3 ==str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
Output results :
The knowledge here is that if a string is a constant string , Then the value of this string cannot be modified , Only use it , So in the case of limited memory space , Just save one copy , Avoid wasting memory space .
This can be likened to a supermarket giving a schoolbag , Schoolbag is compared to memory space , You can go to this supermarket to get anything , Then put it in this schoolbag , But when the bag is full, you can't take it , You should put an ice block inside to cool down , Ice cubes are compared to constant strings , But you will put two pieces ? One piece is enough , Another piece will take up space , Make room for other good things .
So in the title , Pointer to the variable str3 and str4 The value of is the same , The address stored in it is the same , A constant string is stored only in memory space , and str1 and str2 It's not the same , Put the constant string into two arrays , The space requested by the two arrays is different , The addresses of these two spaces must be different , The array name represents the address of the first element , So the addresses corresponding to these two array names must be different , You can compare arrays to schoolbags , Two arrays are two schoolbags , The two schoolbags must be placed in different positions , You have to occupy a place for each to fit .
Two 、 Pointer array
If students are placed in an area , It's called school , If it's a patient , Just call the hospital .
So if you put an integer in the array , It's called an integer array , If you put characters , It is called character array .
If the array contains pointers , It is called pointer array .
3、 ... and 、 Array pointer
1、 Array pointer interpretation
The pointer is a variable , It's a container , It's used to store the address .
If you put cosmetics in a container , It's a makeup bag , If you put school supplies , It's a schoolbag .
If an integer address is placed in a pointer , It's called an integer pointer , This pointer points to an integer data .
If Inside a pointer is the address of the array , It's called array pointer , This pointer points to an array .
* This symbol indicates that a variable is a pointer variable , This symbol is like the north star , Will guide the way , The same goes for the pointer , Will point to a place .
[ ] Represents an array , Two brackets can enclose an area , It represents an array .
[ ] Than * High priority , When they appear at the same time ,[ ] Will give priority to play a role , After all [ ], The larger , It's an array , More power .
So if you want to make * Give priority to play a role , To give * Add a bracket , Give it a yellow jacket , Power came up immediately .
Example :int(*p)[10], Given * A yellow mandarin jacket , therefore p Priority and * combination , Become a pointer , The pointer points to an array , Array has 10 Elements , The type of each element is an integer .
2、 Array name ,& Array name and sizeof(arr)
The array name usually represents the address of the first element
Here, you can imagine that an array is like a coffin , There are many emperors' bodies in it , For future generations to find them , Take the name of the coffin as the address of the first emperor's body .
But there are exceptions , Use sizeof The operator , When operating on array names , In this case, the array name represents the address of the entire array , The calculated value is the size of the entire array .
Use &, Take the address operator , Operate on the array name , At this time, the array name also represents the address of the entire array , So what we get is the address of the whole array .
3、 How array pointers are used
The array name represents the address of the first element , The elements of a two-dimensional array are each row , The first element is the first line , So the address represented by the array name of the two-dimensional array is the address of the first line , Each row can be regarded as a one-dimensional array , So the address of the first element is the address of the array , The address of the array should be put in the array pointer .
The array name represents the array , Just like our names represent us .
Pass parameter to function , Pass the array name of the two-dimensional array to the function , Use array pointers to receive , You can also use a two-dimensional array to receive .
What we imagine here is that the value of address is compared to a soul , It's the soul of a lone soul , The pointer is a body , The soul wants to reincarnate , If it is human soul , It must be thrown into the human body , If it is the soul of an animal , It must be thrown into the animal's body , The address of the array must be put in the array pointer .
#include<stdio.h>
print_arr1(int(*arr)[3],int r,int c)
{
int i = 0;
for(i = 0;i < r;i++)
{
int j = 0;
for(j=0;j<c;j++)
{
printf("%d ",*(*(arr+i)+j));
//arr+i, Corresponding to No i The address of the line ,*(arr+i), What you find is no i That's ok , Each row is a one-dimensional array ,
// So what you find is an array , The array name represents an array , The array name is the address of the first element , Address +i,
// What the representative found is i Addresses of elements .
}
}
}
int main()
{
int arr[3][5] = {
1,2,3,4,5,6,7,8,9,10};
print_arr1(arr, 3, 5);
return 0;
}
Four 、 Array parameter passing and pointer parameter passing
1、 One dimensional array parameters
int arr1[10]={0);
test(arr1);
Function can be used int arr[10] and int arr[ ] and int* p receive .
The number of elements can be omitted , Create according to the number passed .
The array name is the address of the first element , So you can use a pointer to receive .
2、 Two dimensional array parameters
int arr1[10][10]={0};
test(arr1);
Function can be used int arr[10][10], and int arr[ ][10] and int (*p)[10] receive .
The rows of the two-dimensional array used for reception can be omitted , Columns cannot be omitted , Omit column , Arrays will not know how to store elements .
It's like students queuing up on the playground , If you don't tell the students how many rows to line up , Only a few rows , Then you can stand at will .
3、 First level pointer parameter transfer
When the formal parameter is a first-order pointer , When passed to a function , What can be passed ?
You can pass the name of the first level pointer variable , Address , The array name of a one-dimensional array
4、 The secondary pointer transmits parameters
When the formal parameter is a secondary pointer , When passed to a function , What can be passed ?
You can pass the name of the secondary pointer variable , The address of the first level pointer variable , Array name of pointer array
5、 ... and 、 A function pointer
边栏推荐
- Nacos 介绍和部署
- 阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
- Security permission management details
- Date and time function of MySQL function summary
- Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)
- Getaverse, a distant bridge to Web3
- ALV入门
- MySQL eight knowledge points: from getting started to deleting the database
- [acwing] 2983. Toys
- Unnamed Article 33
猜你喜欢

How to reproduce the official course of yolov5 gracefully (II) -- Mark and train your own data set
C语言详解系列——函数的认识(3)形参,实参,嵌套调用和链式访问

Mysql优化

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

MODFLOW Flex、GMS、FEFLOW、HYDRUS实践应用

87. 扰乱字符串

真正的科学减肥

I talked with the interviewer about MySQL optimization in five dimensions

Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health

Getaverse,走向Web3的远方桥梁
随机推荐
Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
Seata submits at details in two stages
C语言-指针进阶
Embedded sharing collection 21
SAP报表开发步骤
手把手教你用代码实现SSO单点登录
SWAT模型在水文水资源、面源污染模拟中的实践技术
Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
基于通用优化软件GAMS的数学建模和优化分析
Excel vba: saving multiple worksheets as new files
SQL注入
Shell的read 读取控制台输入、read的使用
Textfield and password input box that are more flexible and easy to use in compose
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
JVM Lecture 2: class loading mechanism
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology
攻防世界-FlatScience
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题
517. 超级洗衣机
