当前位置:网站首页>Complex entanglement between pointer and array
Complex entanglement between pointer and array
2022-07-18 19:02:00 【Lan Zhou Qianfan】
3、 ... and : Complex entanglement between pointer and array
1: Pointers and arrays
What can pointers do with arrays ? The pointer variable we define can store the address , Then you can store the address of the array !
(1) Point to a one-dimensional array
A simple application
#include<stdio.h>
#include<windows.h>
int main()
{
char str[] = "I love you";
char *target = str;
int count = 0;
while (*target++ !='\0')
{
count++;
/* code */
// printf("%d",count);
}
printf("the total is :%d",count);
system("pause");
}Take this as an illustration
Our side target A pointer is a pointer to an array , See, that is, the first address of the array , It's the characters I The first address , Initialization is like this , When we give the pointer ++ It will point back to the second time in turn . take * Number is to take the value at the address ,ok, I understand. .
There is still a fundamental need to understand how the pointer points , And how to point to an array , So you won't be repeated .
(2) Point to a two-dimensional array
You see, pointing to a two-dimensional array , Let's visualize , You can understand what the array name stands for ?
Printing out the array name will get the first address of the array , That is, the value of the first element .
To find out , Then print verification
#include<stdio.h>
#include<windows.h>
int main(){
int array[2][3] = {0};
for(int i =0;i<2;i++){
for(int j=0;j<3;j++){
scanf("%d",&array[i][j]);
}
}
printf("size of int is %d\n",sizeof(int));
printf("the add of array pointed is %d\n",array);// Let's print out the address according to the decimal system , Is the first address of the entire array
printf("the value of *array pointed is %d\n",*array);// It can be considered to get the address of the nested one-dimensional array
printf("the value of **array is %d\n",**array);// You can think of getting the value of a one-dimensional array
printf("the value of *(array+1) pointed is %d\n",*(array+1));
printf("the add of array+1 pointed is %d\n",array+1);
system("pause");
/*
After output and printing, it can be obtained through analysis ,array Is a pointer to five arrays containing elements
*/
}
/*
How to understand two-dimensional arrays ? Two dimensional arrays are stored in memory according to one-dimensional arrays . It can be considered as nested .
*/I can't write , No more examples . Let's take a look at the interesting knowledge of brain .
2: Pointer array
You may not use much , I admit that it may be outrageous for freshmen . what ? And this thing ?
I can't understand without a picture when necessary ! The picture of little turtle , I brought .
Why is this an array of pointers ? Instead of array pointers ?
[] Priority is higher than *, So first combine p Posterior Union *.
An array of pointers is an array , Each array element holds a pointer variable
What can I do ? You can do this
#include<stdio.h>
#include<windows.h>
int main()
{ // The following is a pointer array
char *p1[5] = {
"every one can dream!",
"i think i can successful",
"dont care it ,just do it for your dream",
"how are you",
"believe yourself"
};
for (int i =0;i<5;i++){
printf("%s\n",p1[i]);
}
system("pause");
}You may have an incomprehensible consciousness , That's how a string can be assigned to a pointer . But that's not what I mean . We understand it this way , After declaring a character pointer , And assign the address of the first character of the string constant to the pointer variable p1[i].
3: Array pointer
Continue to set the baby
So what is the array pointer ?
You can see p and * Parenthesized , So we will give priority to the combination of
An array pointer is a pointer to an array
A simple piece of code
// Here is an example of an array pointer , Pointer to array , Don't think it points to an address , Instead, it points to the entire array
#include<stdio.h>
#include<windows.h>
int main()
{
int temp[] ={1,2,3,4,5};
int (*p2)[5] = &temp;
int i;
for(i =0;i<5;i++)
{
printf("%d\n",*(*p2+i));
}
system("pause");
}The problem is coming. , since p2 Is a pointer , that *p Just · Represents the value , Why take * Well ?
p2 Is pointing to the entire array , We can think of it this way , Proceed to the first floor * It can be considered that it gets the first address of the first element of the array , Again * It can be considered that .ok.
Four : Give you some relevant content and problems encountered
Dolls Give me some code
/* Use the pointer method to save the maximum value to the variable a In the middle , The minimum value is saved to the variable b In the middle */
#include <stdio.h>
void ff(int *p1, int *p2) {// The process of passing the address content of the argument to the past assignment
int a;// and main Function a Not the same
a = *p1;// take p1 The value on the address is passed to a;
// printf("%d", &a);
*p1 = *p2;
*p2 = a;
}
int main() {
int a, b;
int *p_1, *p_2; // Define pointer type variables to point to int Variable of type
scanf("%d %d", &a, &b);
p_1 = &a; // Attach an address to p_1,p_2;
p_2 = &b;
printf("%d,%d\n", &a, &b);
if (a < b) {
ff(p_1, p_2);
}
printf(" The maximum value is %d, The minimum value is %d", a, b);
return 0;
}#include<stdio.h>
#include<windows.h>
int main()
{ // Initialize the two pointers and start pointing to null
int *p1 =NULL;
int *p2 =NULL;
int *p3 =NULL;
int a =6,b =8,c =10;
p1 =&a;// representative p1 Pointer to a The address of
p2 =&b;
p3 =&c;
// You can print *p1/*p2 What does it stand for , In fact, I'll take it again this time * Words , And pointer defined * Dissimilarity , Indicates that the fetch pointer points to the content at the address
// eg:
printf("the value of a is %d\n",*p1);
printf("the address of a is %p\n",p1);// Print the address directly %p
// The exchange value can be directly exchanged
// eg:
*p1 = *p2;
printf("this time ,the value of a is %d\n",a);
//? Try address swapping ?
p1 = p3;
printf("the value of a is %d\n",a);// Print out and you will only find that value exchange is effective , Using a pointer is just a change in the direction of the pointer
printf("the value of pointer p1 is %d",*p1);
system("pause");
}#include<stdio.h>
#include<windows.h>
int main(){
int array[2][3] = {0};
for(int i =0;i<2;i++){
for(int j=0;j<3;j++){
scanf("%d",&array[i][j]);
}
}
printf("size of int is %d\n",sizeof(int));
printf("the add of array pointed is %d\n",array);// Let's print out the address according to the decimal system , Is the first address of the entire array
printf("the value of *array pointed is %d\n",*array);// It can be considered to get the address of the nested one-dimensional array
printf("the value of **array is %d\n",**array);// You can think of getting the value of a one-dimensional array
printf("the value of *(array+1) pointed is %d\n",*(array+1));
printf("the add of array+1 pointed is %d\n",array+1);
system("pause");
/*
After output and printing, it can be obtained through analysis ,array Is a pointer to five arrays containing elements
*/
}
/*
How to understand two-dimensional arrays ? Two dimensional arrays are stored in memory according to one-dimensional arrays . It can be considered as nested .
*/#include<stdio.h>
#include<windows.h>
// Pointer array
int main()
{
char *cBooks[] =
{
"Cease to struggle and you cease to live",
"Time ca heal a broken heart,but it can also break a waiting heart",
"the fox changes his skin but not his habbits",
"Time is but a river flowing from our past"
};
char **jgdabc;
char **jgdabc_loves[4];
jgdabc_loves[0] = &cBooks[0];
jgdabc_loves[1] = &cBooks[1];
jgdabc_loves[2] = &cBooks[2];
jgdabc_loves[3] = &cBooks[3];
printf("%d\n",jgdabc_loves[0]);
printf("%d\n",*jgdabc_loves);
printf("%s\n",*jgdabc_loves[0]);
printf("%c\n",**jgdabc_loves[0]);
printf("%c\n",**jgdabc_loves[1]);
system("pause");
}Here, please run by yourself and think , Focus on the main points of the pointer , And pointing to the pointer · The meaning of the pointer .
边栏推荐
- Pycharm上Modify Run Configuration的使用方法,带参数配置
- Preparing transaction:done Verifying transaction:failed RemoveError:‘requests‘ is a dependency of **
- [ CTF ] Reverse baby_re
- 多端口和单臂路由器互联VLAN实验
- [Huang ah code] getting started with MySQL - 1. SQL execution process
- js 地图两点距离计算
- 2021年11月软考网络规划设计师上午真题及答案解析
- [LeetCode解题报告] 423. 从英文中重建数字
- Conversion of data types
- 华为ensp网络之如何实现集线器和换机工作理验证实验?
猜你喜欢

Offline installation: how to build a secure enterprise class harbor service? The content is too detailed.

Based on data report processing system (vue+ssm+mysql)

unity 关于使用Rigidbody的Addforce但不起作用的一些可能原因以及解决方法

TCP congestion control details | 6 Active queue management

(手工)【sqli-labs50-53】order by注入

HCIP(5)

Babbitt | metauniverse daily must read: fraud of 200million in half a year, the biggest "hidden worry" of digital people, and deepfake fraud detonated the Internet again

HCIP(6)

Solution of eigenvalue and eigenvector

HCIP(5)
随机推荐
Learning path PHP -- post can't get the requested data
Conception et mise en œuvre d'un système crawler basé sur le Web
2022.7.4-7.10 AI行业周刊(第105期):蜗牛
# bayesian network ## Laplace approximation
Templates are used in generic programming - Extraction Technology - to obtain element type examples through container types
Ruffian Heng embedded bimonthly issue 58
Flink CEP - complex event processing
Graph Neural Networks
Decoding of relative motion of two moving points
Know JVM
Solution of eigenvalue and eigenvector
[原创]移远RM500U-CN模组驱动移植
Docker配置mysql以及宿主机容器目录挂载
ospf特殊区域
Keywords some, option in rust
2022新年祝福代码诠释(1)
【C语言刷LeetCode】134. 加油站(M)
Breadth first traversal of Graphs
[ CTF ]MISC encode
Solution of eigenvalue and eigenvector