当前位置:网站首页>Little white battle pointer (Part 1)
Little white battle pointer (Part 1)
2022-06-29 07:48:00 【Mertrix_ ITCH】
One 、 Getting to know the pointer
1. Pointer overview :
The pointer : An address in memory .
Pointer to the variable : Dedicated to Storage variable address The variable of .
1.1 Definition Format : Type specification * Variable name
eg:int *p
( Type specification : Determined by the data type pointed to by the pointer variable . *--- Indicates that the variable is a pointer variable )
1.2 assignment Format :(& Variable name )
a.int a; int *p=&a [ Assign values while defining ]
b.int a; int *p; p=&a; [ Define before assign !!! Do not add "*"]
You must assign a value before using a pointer variable , And assigning a value to a pointer variable can only assign an address .
3. A reference to a variable pointer Format :* Pointer to the variable
Its meaning is to refer to the value pointed to by the pointer variable .
#include<stdio.h>
int main()
{
int a,b;
printf(" Yes, please. a、b Assign initial value to :\n");
scanf("%d%d",&a,&b);
//int *p,*q; Defining pointer variables p,q.
int *p=&a,*q=&b; // Assign values while defining pointer variables
//p=&a,q=&b; Assign values to pointer variables
printf("a=%d,b=%d",*p,*q); // Reference pointer variables
return 0;
}
2."&" and “*”
&: Fetch address , A unary operator that returns the address of an operand ;
*: Pointer operator , Returns the value of the variable in the specified address ;
" &* “ and " * & " (”&“ and ”*" Operators have the same precedence , associativity : From right to left .)
With int num , *p ; p = &num ; For example
1. &*p = &(*p) =p 【 Take variables num Address 】
2. *&num = *(&num)=num 【 Take variables num Value 】
#include<stdio.h>
int main() {
long i;
long *p;
printf("please input the number:\n");
scanf("%ld",&i);
p=&i;
printf("the r1 is:%d\n",&*p); /* Output variables i The address of */
printf("the r2 is:%d\n",&i); /* Output variables i The address of */
printf("the r3 is:%d\n",*&i); /* Output variables i Value */
printf("the r4 is:%d\n",i); /* Output variables i Value */
printf("the r5 is:%d\n",*p); /* Output variables as pointers i Value */
}
3. Pointer self increment and self decrement operation
The self increasing and self decreasing operation of the pointer is Increase or decrease by the direct length of the data type it points to .
Example:
short i=4;
short *p=&i;
p++; //short- Occupy 2 Bytes ,p The value of the increase 2(2 Bytes )
#include<stdio.h>
int main(){
int i;
int *p;
printf("please input the number:\n");
scanf("%d",&i);
p=&i;
printf("the result1 is %d\n",p);
p++; // Point to the next store int Address of type number ,p The value of the increase 4(4 Bytes )
printf("the result2 is %d\n",p);
}
Two 、 One dimensional arrays and pointers
1. When defining a one-dimensional array , The system will allocate storage space for the array in memory , The array name is the first address of the array .
1. When defining a pointer variable , And pass the first address of the array to the pointer variable , Then the pointer points to the one-dimensional array .
eg:int *p,a[5];
p=a || p&a[0]; // Will array a First address assigned to p
2. Reference a one-dimensional array through a pointer
eg: *(p+i) || *(a+i)) //i: Is a cyclic variable , Print array elements by moving the pointer with a loop statement
Optimize : printf("%d\t",*p++) // Indicates that the pointer movement can use "++" or "--" Operator
#include<stdio.h>
int main() {
int *p,*q,a[5],b[5];
p=&a[0];
q=b;
printf("please input array a:\n");
for(int i=0; i<5; i++) {
scanf("%d",&a[i]);
}
printf("please input array b:\n");
for(int i=0; i<5; i++) {
scanf("%d",&b[i]);
}
printf("array a is:\n");
for(int i=0; i<5; i++)
printf("%-5d",*p++); // use p++ Control pointer movement
printf("\n");
printf("array b is:\n");
for(int i=0; i<5; i++)
printf("%5d",*(b+i)); // Output array elements ,*(q+i) or *(b+i)
printf("\n");
}
Case optimization :
When an array is assigned : use (p++) Enter the next address to store the basic integer .
@!!! If... Is used for output at this time *p++, Then reset the pointer variable p Pointing to the array a The first address .
Example:for(int i=1;i<5;i++)
scanf("%d", p++ || &a[i]);
#include<stdio.h>
int main(){
int *p,*q,a[5],b[5];
p=&a[0];
q=b;
printf("please input array a:\n");
for(int i=0; i<5; i++) {
scanf("%d",p++); //p++: Point to the next address to store the basic integer .
}
printf("please input array b:\n");
for(int i=0; i<5; i++) {
scanf("%d",q++);
}
q=a; /*@ At this time must be : Put the pointer variable p、q Relocate to array a、b Starting position */
q=b;
printf("array a is:\n");
for(int i=0; i<5; i++)
printf("%-5d",*p++); // use *p++ Control pointer movement
printf("\n");
printf("array b is:\n");
for(int i=0; i<5; i++)
printf("%5d",*q++); // Output array elements ,*(q+i) or *(b+i)
printf("\n");
}

边栏推荐
- Viewing application and installation of Hana database license
- Appium自动化测试基础 — ADB常用命令(三)
- tf.compat.v1.global_variables
- Alicloud access resource: nosuchkey
- 从Nacos客户端谈Nacos配置中心
- 数组知识点小结
- Explanation of swing transformer theory
- Detailed explanation of route (Jiuyang Scripture)
- Schnuka: visual positioning system manufacturer what is a visual positioning system
- KingbaseES 中select distinct on 语句
猜你喜欢

呕心沥血总结出来的MySQL常见错误以及解决方法(二)

呕心沥血总结出来的MySQL常见错误以及解决方法(一)

九州云助力内蒙古“东数西算”工程,驱动测绘行业智慧新生态

SQL 注入绕过(六)

Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
What tools do testers need to know
![[translation] E-Cloud. Large scale CDN using kubeedge](/img/ac/178c078589bb5bc16dbdc8f4ae9525.png)
[translation] E-Cloud. Large scale CDN using kubeedge

手把手系列---安装SpotBugs、并快速上手使用

面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?

Schnuka: what is visual positioning system? How visual positioning system works
随机推荐
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
呕心沥血总结出来的MySQL常见错误以及解决方法(二)
What is a Test Architect
Markdown skill tree (8): code blocks
What you should know about databases
帆船动力学仿真分析
SQL injection bypass (6)
Listen to textarea input through Keyup to change button style
The table cannot be vacuumed because the cursor is open for a long time
Detailed explanation of shell condition judgment
Markdown skill tree (9): tables
【工控老马】PLC六路抢答器系统设计详解
道闸控制器通讯协议
MFC中利用CDockablePane实现悬浮窗
Appium自动化测试基础 — ADB常用命令(二)
cv2.cvtColor
Cv:: mat and Base64 conversion (including picture compression and decompression)
Loop nesting: why can large loops inside and small loops outside improve the running efficiency of programs
【工控老马】洗衣机PLC程序控制系统设计详解
Schnuka: visual positioning system manufacturer what is a visual positioning system