当前位置:网站首页>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 !
边栏推荐
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- Floating point comparison, CMP, tabulation ideas
- 2-year experience summary, tell you how to do a good job in project management
- 2.初识C语言(2)
- 13 power map
- 1.C语言初阶练习题(1)
- First acquaintance with C language (Part 1)
- Database operation of tyut Taiyuan University of technology 2022 database
- Set container
- 2.C语言矩阵乘法
猜你喜欢
6.函数的递归
Small exercise of library management system
13 power map
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
String class
String类
Interview Essentials: talk about the various implementations of distributed locks!
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
8.C语言——位操作符与位移操作符
随机推荐
TYUT太原理工大学2022数据库考试题型大纲
Design a key value cache to save the results of the most recent Web server queries
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
系统设计学习(三)Design Amazon‘s sales rank by category feature
更改VS主题及设置背景图片
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
[中国近代史] 第六章测验
1.C语言初阶练习题(1)
First acquaintance with C language (Part 1)
TYUT太原理工大学2022“mao gai”必背
学编程的八大电脑操作,总有一款你不会
Inheritance and polymorphism (I)
MPLS experiment
167. Sum of two numbers II - input ordered array - Double pointers
TYUT太原理工大学2022数据库大题之分解关系模式
13 power map
5. Download and use of MSDN
图书管理系统小练习
Common method signatures and meanings of Iterable, collection and list
IPv6 experiment