当前位置:网站首页>Mortal immortal cultivation pointer-2
Mortal immortal cultivation pointer-2
2022-07-06 13:27:00 【Programmer rock】
6、 ... and 、 Pointer to the foundation period
This section , We will use the pointer to visit the monster trading market , Quickly master the advanced usage of pointer .
Catalog
6、 ... and 、 Pointer to the foundation period
The legal use of arrays as pointers
Illegal use of arrays as pointers
Arrays and pointers “ size ” The difference between
Free pointer - Look around , Violent bargaining
pointer to const - Buddhism department goes shopping in the black market , Just look, not bargain
Constant pointer - Look at a monster , Just for it
Constant pointer to constant - A dedicated customer who doesn't need money
Definition and basic use of pointers to arrays
Addition and subtraction of pointers to arrays
6.4 The parameters of the function pass an array
The parameters of the function pass a one-dimensional array
The parameters of the function pass a two-dimensional array (4 Ways of planting )
6.1 Pointers and arrays
Pointers are used as arrays
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int* p = &prices[0]; //prices;
for (int i = 0; i < 8; i++) {
printf(" The first %d The price of level 1 monster is :%d Lingshi \n", i + 1, p[i]); // Use pointers in the form of arrays
}
Summary : For the pointer p, The compiler the p[i] Understood as a : *(p+i)
The legal use of arrays as pointers
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
*prices = 150; // Arrays are used with pointers
printf("%d\n", prices[0]); // 150
Illegal use of arrays as pointers
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int x;
prices = &x; // Array names cannot point to other locations , Similar to a “ Special constant pointer ”
Arrays and pointers “ size ” The difference between
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int* p = prices;
printf("%d, %d", sizeof(p), sizeof(prices));
// Output 4,32 perhaps 8,32
// stay 32 Bit system (x86), Pointer occupied 4 Bytes of memory
// stay 64 Bit system (x64), Pointer occupied 8 Bytes of memory
Array name , What is it
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
// 1. The value of the array name , That's the number... Of the array 0 Addresses of members
printf("%x, %x\n", prices, &prices[0]);
// 2. Access to data , Array name , Similar to a constant pointer , You can't change the direction
// here ,p and prices Very similar
int* const p = &prices[0]; //const The usage of will be explained in detail later
// 3. For memory size , Arrays and types are different
printf("%d, %d", sizeof(p), sizeof(prices));
6.2 Pointers and const
In the monster black market of Xiuxian world , Use the pointer to browse the black market , coordination const, You can get different results .
Free pointer - Look around , Violent bargaining
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int* p = prices;
p += 2; // Change direction
*p -= 100; // Forcibly change the price
printf("%d\n", *p); //400
pointer to const - Buddhism department goes shopping in the black market , Just look, not bargain
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
const int* p = prices;
p += 2; // You can change p The direction of
*p -= 100; // Compile failed ! Cannot change the content pointed
Constant pointer - Look at a monster , Just for it
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int * const p = prices;
p += 2; // Compile failed ! You can't change the direction
*p -= 50; // You can bargain ( Can change the data pointed )
Constant pointer to constant - A dedicated customer who doesn't need money
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
const int * const p = prices;
p += 2; // Compile failed ! You can't change the direction
*p -= 50; // Compile failed ! Don't bargain , The data pointed to cannot be changed
6.3 Pointer to array
Let's analyze the longevity of immortals at all levels , By the way, master the usage of pointing to arrays .
Lower realm
Qi training period ( Absorb the aura of heaven and earth , Into the body into the element force , Longevity can reach a hundred years , It can fly in a short time )
Foundation period ( Dantian is liquid , Kepigu , Up to 200 years old , Cannot escape the light , Can fly a sword for a long time )
Jindan period ( There are regular round solid Dan yuan in the Dantian , Opened Valley , Longevity can reach 500 years , You can fly by light )
Primipara ( Broken pills become babies , Longevity can reach thousands of years , Light flight , Broken body baby can teleport )
Upper realm
The transformation period ( The body is doubled , Yuanying transits to Yuanshen , More than 2000 years , Master Yuan Li , Can break Yuan Ying's blink )
The period of deficiency ( Shouyuan can reach nearly 5000 years old , Yuanying in the body turns into Yuanshen , Return to the virtual body , Into separate bodies or multiple separate bodies )
Syncytial stage ( Separation and noumenon are one , To return to simplicity , You can break thousands of incarnations of refining emptiness , Longevity can last for tens of thousands of years )
Mahayana ( Up to 20000 years , Skillfully use or create their own magic powers , The mana body is ready to fly , Chen is a monk with great power )
Definition and basic use of pointers to arrays
int ages[2][4] = {
100, 200, 500, 1000, //ages[0] In the lower realm 4 An array of levels of life
2000,5000,10000,20000 //ages[1] In the upper realm 4 An array of levels of life
};
int(*p)[4];
// Defines a pointer to an array p
//p Can only point to : contain 4 Members , Every member is int An array of types
// Pay attention to the grammar ,* Must be in ( ) Inside
p = &ages[0]; //ages
// Through the pointer to the array , To access the members in the array
for (int i = 0; i < 4; i++) {
printf("%d ", (*p)[i]); // p[0][i]
}
// The size of the pointer to the array
printf("\n %d %d \n", sizeof(p), sizeof(*p));
Addition and subtraction of pointers to arrays
int ages[2][4] = {
100, 200, 500, 1000, //ages[0] In the lower realm 4 An array of levels of life
2000,5000,10000,20000 //ages[1] In the upper realm 4 An array of levels of life
};
int(*p)[4] = &ages[0]; //ages
p++; // p Points to the next array !
printf("%d\n", (unsigned int)(p + 1) - (unsigned int)p); //16
for (int i = 0; i < 4; i++) {
printf("%d ", (*p)[i]); // 2000
}
6.4 The parameters of the function pass an array
The parameters of the function pass a one-dimensional array
Calculation 1-8 The average price of level monster .
#include <stdio.h>
int calcAveragePrice1(int p[8]) {
// Particular attention : here p Arrays are not real arrays , It's actually a pointer int *p
printf("%d\n", sizeof(p));
int s = 0;
for (int i = 0; i < 4; i++) {
s += p[i];
}
return s / 4;
}
int calcAveragePrice2(int p[], int n) {
int s = 0;
for (int i = 0; i < 4; i++) {
s += p[i];
}
return s / 4;
}
int calcAveragePrice3(int *p, int n) {
int s = 0;
for (int i = 0; i < 4; i++) {
s += p[i];
}
return s / 4;
}
int main(void) {
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
// Calculation 1-8 The average price of level monster
printf("%d\n", calcAveragePrice1(prices)); //ages;
printf("%d\n", calcAveragePrice2(prices, 8)); //ages;
printf("%d\n", calcAveragePrice2(prices, 8)); //ages;
return 0;
}
The parameters of the function pass a two-dimensional array (4 Ways of planting )
Calculate the average life span of monks at all levels .
#include <stdio.h>
int calcAverageAge1(int ages[2][4]) {
// Particular attention : here ages Not a real two-dimensional array
// It's actually a pointer to a one-dimensional array int (*ages)[4]
printf("%d\n", sizeof(*ages));
int s = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
s += ages[i][j];
}
}
return s / (2 * 4);
}
int calcAverageAge2(int ages[][4], int n) {
// Particular attention : here ages Arrays are not real arrays
// It's actually a pointer to a one-dimensional array int (*ages)[4]
printf("%d\n", sizeof(*ages));
int s = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
s += ages[i][j]; // *(*(ages+i) + j)
}
}
return s / (2 * 4);
}
int calcAverageAge3(int(*ages)[4], int n) {
printf("%d\n", sizeof(*ages));
int s = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
s += ages[i][j]; // *(*(ages+i) + j)
}
}
return s / (2 * 4);
}
int calcAverageAge4(int* ages, int m, int n) {
int s = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
s += *(ages + i * n + j);
}
}
return s / (2 * 4);
}
int main(void) {
int ages[2][4] = {
100, 200, 500, 1000, //ages[0] In the lower realm 4 An array of levels of life
2000,5000,10000,20000 //ages[1] In the upper realm 4 An array of levels of life
};
printf("%d\n", calcAverageAge1(ages));
printf("%d\n", calcAverageAge2(ages, 2));
printf("%d\n", calcAverageAge3(ages, 2));
printf("%d\n", calcAverageAge4(ages, 2, 4));
return;
}
“C Language and C++ in , You can't directly put a two-dimensional array ( Or more multidimensional arrays ), As a function parameter of general form , You cannot pass a normal multidimensional array to a function .” Quote from 《C Experts programming 》 The first 10 Chapter of 5 section .
But we can use the above example code 4 Two forms to pass two-dimensional arrays .
6.5 Function returns an array
Define a function , Calculate , Returns an array , The array contains the lowest and highest prices of monsters at all levels , And the average price .
int(*calc(int prices[], int n))[3]{
int s = 0;
for (int i = 0; i < n; i++) {
s += prices[i];
}
int(*ret)[3]; // Define a pointer to an array
ret = malloc(3 * sizeof(int));
(*ret)[0] = prices[0];
(*ret)[1] = prices[n - 1];
(*ret)[2] = s / n;
return ret;
}
int main(void) {
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int(*p)[3] = calc(prices, 8);
printf(" The lowest price =%d, The highest price =%d, The average price =%d", (*p)[0], (*p)[1], (*p)[2]);
return;
}
Use typedef Pointer to array , To optimize :
typedef int(*threeValueArray_t)[3];
threeValueArray_t calc(int prices[], int n){
int s = 0;
for (int i = 0; i < n; i++) {
s += prices[i];
}
int(*ret)[3]; // Define a pointer to an array
ret = malloc(3 * sizeof(int));
(*ret)[0] = prices[0];
(*ret)[1] = prices[n - 1];
(*ret)[2] = s / n;
return ret;
}
int main(void) {
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
threeValueArray_t p = calc(prices, 8);
printf(" The lowest price =%d, The highest price =%d, The average price =%d", (*p)[0], (*p)[1], (*p)[2]);
return;
}
thus , We have mastered the usage of the pointer in the foundation period , The golden elixir period is about to advance !
边栏推荐
- MPLS experiment
- Tyut Taiyuan University of technology 2022 introduction to software engineering
- MySQL Database Constraints
- E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
- Wei Pai: the product is applauded, but why is the sales volume still frustrated
- Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
- 4. Binary search
- Design a key value cache to save the results of the most recent Web server queries
- View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
- 继承和多态(下)
猜你喜欢
2.C语言矩阵乘法
3.C语言用代数余子式计算行列式
MPLS experiment
图书管理系统小练习
Database operation of tyut Taiyuan University of technology 2022 database
TYUT太原理工大学2022数据库大题之E-R图转关系模式
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
西安电子科技大学22学年上学期《基础实验》试题及答案
7. Relationship between array, pointer and array
随机推荐
1.C语言初阶练习题(1)
Design a key value cache to save the results of the most recent Web server queries
2.C语言初阶练习题(2)
C language to achieve mine sweeping game (full version)
【话题终结者】
2. C language matrix multiplication
2-year experience summary, tell you how to do a good job in project management
View UI plus released version 1.2.0 and added image, skeleton and typography components
Implement queue with stack
[中国近代史] 第六章测验
西安电子科技大学22学年上学期《基础实验》试题及答案
View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
MySQL Database Constraints
Cloud native trend in 2022
2.C语言矩阵乘法
What are the advantages of using SQL in Excel VBA
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
TYUT太原理工大学往年数据库简述题
Inheritance and polymorphism (Part 2)