当前位置:网站首页>C language pointer and array - learning 23
C language pointer and array - learning 23
2022-06-12 00:39:00 【XG. Solitary dream】
This paper is finally updated at 2022 year 02 month 17 Japan , Has more than 11 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !
Pointer variables as function parameters
- The parameters of a function can be not only integers 、 floating-point 、 Data such as character type , It can also be a pointer type .
- Its function is to The address of one variable is passed to another function .
- for example :
void swap(int *a,int *b)
#include <stdio.h>
void main() {
void swap(int *x, int *y);
int a,b;
int *a1 = &a;
int *b1 = b;
printf(" Please enter a,b Value :\n");
scanf_s("%d %d", &a, &b);
if (a < b) {
swap(a1, b1);
}
printf("max = %d, min=%d\n", a, b);
}
void swap(int *x, int *y) {
int p;
p = *x;
*x = *y;
*y = p;
}- Be careful : Function call Sure ( And only ) obtain A return value ( Functional value ), While using Pointer to the variable As a parameter , Sure Get multiple changed values . It's hard to do this without pointer variables .
- If you want to get n Values to change
- 1. stay In the main function, there is n A variable , use n Pointer variables point to them ;
- 2. Design a function , Yes n Pointer parameters . In this Function to change this n Values of parameters ;
- 3. Call this function in the main function. , When called, this n Pointer variables as arguments , Put their Address Pass it on to The formal parameter of the function ;
- 4. During the execution of this function , Pointer variable through parameter , Change what they point to n Values of variables ;
- 5. You can use these variables that change the value in the main function .
- Example
Input 3 It's an integer a,b,c, It is required to output them in the order of large to small . Implement with function .
#include <stdio.h>
void main() {
void exchange(int *x, int *y,int *z);
int a,b,c;
int *a1 = &a;
int *b1 = &b;
int *c1 = &c;
printf(" Please enter a,b,c Value :\n");
scanf_s("%d %d %d", &a, &b, &c);
exchange(a1,b1,c1);
printf("%d, %d, %d\n", a,b,c);
}
void swap(int *x, int *y) {
int p;
p = *x;
*x = *y;
*y = p;
}
void exchange(int *x, int *y, int *z) {
void swap(int *x, int *y);
if (*x < *y) {
swap(x, y);
}
if (*x < *z) {
swap(x, z);
}
if (*y < *z) {
swap(y, z);
}
}Pointer to array element
- A variable has an address , An array contains several elements , Each array element has a corresponding address .
- Pointer variables can point to array elements ( Put the address of an element into a pointer variable ).
- The pointer to the array element is the address of the array element .
Operation of pointer when referring to array elements
- When the pointer points to an array element , The following operations are allowed :
- One plus an integer ( use + or +=), for example :
p + 1 - One minus an integer ( use - or -=), for example :
p - 1 - Subtract oneortwo pointers , Such as p1-p2 ( Only p1 and p2 All points to elements in the same array )
- One plus an integer ( use + or +=), for example :
- If the pointer variable p Pointed to an element in the array
- p+1 Point to the next element in the same array ;
- p-1 Points to the previous element in the same array .
- If p The initial value for the &a[0], be
- p+i and a+i It's an array element a[i] The address of ,
- Or say , They point to a The array number is i The elements of .
- for example
#include <stdio.h>
void main() {
int a[5] = {1,2,3,4,5};
int *a1 = &a[0]; // perhaps int *a1 = a
printf("%d, %d\n", *a1, a1);
a1 = a1 + 2;
printf("%d, %d\n",*a1, a1);
}- When p Point to a When the first element of the array , Array elements a[i] There are four kinds of address expressions for :
&a[i]a + ip + i&p[i]
- When p Point to a When the first element of the array , Array elements a[i] There are four kinds of value expressions for :
a[i]*(a + i)*(p + i)p[i]
- a and p There are essential differences :
- a It's a pointer constant , Its value is immutable ;
- p It's a pointer variable , Its value is variable .
- therefore ,a++、a = p And so on are illegal expressions ;
- and p++、p = a They're all legal expressions .
- If The pointer p1 and p2 All point to the same array
- p2 - p1 To calculate p2 The element referred to And p1 The element referred to How many elements are there between .
- Subtract two pointer variables The meaning is The difference between the two addresses Divide Number of bytes per array element .
- Example
#include <stdio.h>
void main() {
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *p1 = &a[0];
int *p2 = &a[6];
printf("p1 The address of :%d\n",p1);
printf("p2 The address of :%d\n",p2);
printf("%d\n", p2 - p1);
printf("%d\n",*p2 - *p1); // int by 4 Bytes
}*++p, Equivalent to*(++p), First of allp+1namely Point to the next group element , Retake*pAs the value of this expression .*p++, Equivalent to*(p++), takepAs such Value of expression , Then makep+1.(*p)++, FirstpThe point is Element value As The value of this expression , then The element value plus 1.++(*p), takepThe point is Element value plus 1 As The value of this expression .
#include <stdio.h>
void main() {
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *p = &a[0];
printf("%d\n",*p);
printf("%d\n",*++p);
printf("%d\n",*p++);
printf("%d\n",(*p)++);
printf("%d\n",++(*p));
}Reference array elements through pointers
- Reference an array element , You can use the following two methods :
- 1. The subscript method , for example :
a[i] - 2. Pointer to the method , for example :
*(a+i)or*(p+i)among a It's an array name ,p Is a pointer variable to an array element , Its initial value p = a
- 1. The subscript method , for example :
- Example
There is an integer array a, Yes 10 Elements , Require all elements in the output array .
#include <stdio.h>
void main() {
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *p;
// The subscript method
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n");
// Calculating array element address by array name
for (int i = 0; i < 10; i++) {
printf("%d ", *(a+i));
}
printf("\n");
// Pointer to the method
for (p=a; p <a+10; p++) {
printf("%d ", *p);
}
printf("\n");
}
- 3 Comparison of two methods :
- The first (1) And the (2) Methods Same execution efficiency
- C Compiling system is to
a[i]Convert to*(a+i)To deal with the , That is, calculate the element address first . - Therefore, the (1) And the (2) Two ways to find array elements More time-consuming .
- C Compiling system is to
- The first (3) Methods (1)、 The first (2) Fast method
- Point directly to an element with a pointer variable , You don't have to recalculate the address every time .
- such Change address values regularly for example
p++Can greatly improve the efficiency of implementation .
- Using subscript method intuitive , Can directly know the number of elements .
- It is not intuitive to use address method or pointer variable method , It is difficult to quickly determine which element is currently being processed .
- The first (1) And the (2) Methods Same execution efficiency
边栏推荐
- The latest report of Xinsi technology shows that 97% of applications have vulnerabilities
- How to make scripts executable anywhere
- 组态王如何利用无线Host-Link通信模块远程采集PLC数据?
- "Failure" of the prospectus of Laowang: Laowang made its first dash for listing in Hong Kong, and the turnover rate continued to decline
- 環境搭建2
- 汛期化工和危险品企业如何加强防控重大安全风险
- 手机wps如何压缩文件打包发送
- 月份选择器禁用当月以后的数据 包含当月
- Cube technology interpretation | detailed explanation of cube applet Technology
- Sword finger offer 09 Implementing queues with two stacks
猜你喜欢

What are the advantages of Tiktok applet

Construction environnementale 2

Pass the command parameters for operating mongodb to Mongo in the shell

2023 spring recruit | ant group middleware Intern Recruitment

Why are the values of ordereddict not equal- Why are the values of an OrderedDict not equal?

Month selector disable data after the current month to include the current month

苹果手机wps文件如何发送到qq邮箱

How to make scripts executable anywhere

UVM: transaction level modeling (TLM) 1.0 communication standard

汛期化工和危险品企业如何加强防控重大安全风险
随机推荐
Redis master-slave replication, sentinel mode and cluster
苹果手机wps如何改字体大小
Is the o2o platform worth doing in 2022
gin解决跨域问题
IP编址概述
干货|一次完整的性能测试,测试人员需要做什么?
Pycharm file name taboo
Global and Chinese chromatographic silica gel resin industry research and investment direction forecast report 2022 Edition
Oracle uses Navicat tool to import and export data
详解异步任务:函数计算的任务触发去重
出门带着小溪
No permission to mount SMB share prompt directory
wps表格怎么取消智能表格样式
Microservice automation
Experiment 7 class construction and static member function
環境搭建2
JS -- prevent automatic recovery of page position
UVM: transaction level modeling (TLM) 1.0 communication standard
多年测试菜鸟对黑盒测试的理解
The "hard words" about interface testing