当前位置:网站首页>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]); // 150Illegal 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 !

边栏推荐
- 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
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- Redis cache obsolescence strategy
- 【快趁你舍友打游戏,来看道题吧】
- 2. C language matrix multiplication
- 4.30动态内存分配笔记
- Small exercise of library management system
- arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
- Network layer 7 protocol
- 最新坦克大战2022-全程开发笔记-3
猜你喜欢

最新坦克大战2022-全程开发笔记-2

Data manipulation language (DML)

MPLS experiment

Tyut Taiyuan University of technology 2022 introduction to software engineering summary

2.初识C语言(2)

阿里云微服务(三)Sentinel开源流控熔断降级组件

Alibaba cloud microservices (IV) service mesh overview and instance istio

Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology

5. Function recursion exercise

西安电子科技大学22学年上学期《射频电路基础》试题及答案
随机推荐
MySQL Database Constraints
2.C语言矩阵乘法
4.30动态内存分配笔记
13 power map
6. Function recursion
Several high-frequency JVM interview questions
TYUT太原理工大学2022数据库大题之E-R图转关系模式
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
西安电子科技大学22学年上学期《信号与系统》试题及答案
继承和多态(上)
View UI plus released version 1.3.1 to enhance the experience of typescript
Solution: warning:tensorflow:gradients do not exist for variables ['deny_1/kernel:0', 'deny_1/bias:0',
First acquaintance with C language (Part 1)
String类
TYUT太原理工大学2022“mao gai”必背
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
2. Preliminary exercises of C language (2)
JS interview questions (I)
学编程的八大电脑操作,总有一款你不会
继承和多态(下)