当前位置:网站首页>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");
}

边栏推荐
- TF. Repeat and stack operations of slim
- KingbaseES应对表年龄增长过快导致事务回卷
- tf. count_ nonzero
- 1032 Sharing
- Cross domain data request using jsonp
- SQL injection bypass (6)
- Schnuka: what is visual positioning system? How visual positioning system works
- Detailed explanation of top and free commands
- matlab simulink 电网扫频仿真和分析
- Suggestions on working methods and efficient work
猜你喜欢

Mmclassification installation and debugging

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

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?

Wechat applet learning notes (summer vacation)

1032 Sharing

Schnuka: 3D visual inspection scheme 3D visual inspection application industry

Explain canfd message and format in AUTOSAR arxml in detail

Appium environment setup

matlab simulink 电网扫频仿真和分析

tf.count_nonzero
随机推荐
Appium automation test foundation ADB common commands (III)
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
Oracle 批量插入数据-插入民族数据
358. K distance interval rearrange string sorting
Schnuka: visual positioning system manufacturer what is a visual positioning system
C实战——高配版贪吃蛇游戏设计
How to talk about salary correctly in software test interview?
How to view software testing training? Do you need training?
Suggestions on working methods and efficient work
Appium 环境搭建
【量化投资系统】问题记录及解决方法
Roblox剑九之剑二
多态中的向上和向下转型
Detailed explanation of route (Jiuyang Scripture)
AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放
1183: patient queue
帆船动力学仿真分析
[translation] swarmed out. Design methods for building modern applications
电检码配置
Alicloud access resource: nosuchkey