当前位置:网站首页>C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)
C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)
2022-06-12 14:24:00 【Mortal programming biography】
After learning from the previous chapter , You must have gained a lot . So this chapter mainly takes you to c A difficult chapter of language , That's the pointer .
1. The concept of pointer : A pointer is a variable , It's used to store the address .
2. The size of the pointer : stay 32 Bit platform unified bit 4 Bytes , stay 64 The bit platform pointer is unified as 8 Bytes .
3. The type of pointer : Yes int* ,char*,float* wait , And they can store different types of addresses . The following code :
#include<stdio.h>
int main()
{
int a=10;
char* pc=&a;
return 0;
}There is no grammatical problem with this kind of writing , So many people want to know , Since any pointer type can store different types of addresses , So why distinguish between types ? because , The pointer type determines the length of bytes that can access the pointer storage address ,int* Type can access the stored address 4 Bytes , and char* Can access the stored address 1 Byte length , And so on .
Two 、 Character pointer
seeing the name of a thing one thinks of its function , Is a pointer variable used to store characters , But it seems simple , But there are also many potholes , The following code :
#include<stdio.h>
int main()
{
const char* pstr="hello world";
printf("%s",pstr);
return 0;
}This code is particularly easy to think of as putting a string into a character pointer pstr in , But the essence is to put the string hello world, The address of the first character is put in pstr in .
3、 ... and 、 Classic title
example 1:
#include<stdio.h>
int main()
{
char arr1[]="abcdef";
char arr2[]="abcdef";
char* p1="abcdef";
char* p2="abcdef";
if(arr1==arr2)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}Is it output hehe still haha Well ?
analysis : We created 2 A character array and a character pointer are stored abcdef,arr1==arr2?, Although the strings stored in the array are consistent , But neither of them belongs to the same memory space , In other words : Although you are the same as your neighbor , But is the house number the same ? So the result is printed haha. Then, let's change the result of the establishment conditions ?
#include<stdio.h>
int main()
{
char arr1[]="abcdef";
char arr2[]="abcdef";
char* p1="abcdef";
char* p2="abcdef";
if(p1==p2)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}p1 and p2 It's all pointers , At the same time point to abcdef It also means that the memory address is the same, so the equality condition holds , Print hehe.
Four 、 Pointer array
1. Concept : It's essentially an array , It's not a pointer , Is an array used to store pointers
Such as :
#include<stdio.h>
int main()
{
int*arr[10];
return 0;
}2. Basic usage of pointer array
#include<stdio.h>
int main()
{
int arr1[]={1,2,3,4,5};
int arr2[]={2,3,4,5,6};
int arr3[]={3,4,5,6,7};
int* paar[]={arr1,arr2,arr3};
int i=0;
for(i=0;i<3;i++)
{
int j=0;
for(j=0;j<5;j++)
{
printf("%d ",*(parr[i]+j));
}
}
return 0;
}This code uses the pointer array to save three arrays , Be careful : Because the address of the first element is stored in the pointer array ( Array name ), So traversing the array is a loop .
5、 ... and 、 Array pointer
1. Concept : As opposed to pointer arrays , It's essentially a pointer , Is a pointer to an array .
Such as :
#include<stdio.h>
int main()
{
int arr[10]={0};
int (*pa)[10]=&arr; // Array pointer
return 0;
}*pa Enclosed in parentheses because [] The priority ratio * high , This array pointer means : It's a pointer , This pointer points to an array , The array has ten elements , Each element type is int, It's an array pointer .
2. Usage of array pointers :
#include<stdio.h>
void print(int(*p)[5],int x,int y)
{
int i=0;
for(i=0;i<x;i++)
{
int j=0;
for(j=0;j<y;j++)
{
printf("%d ",*(*(p+i)+j));
}
printf("\n"); // Inner loop traverses the array once and then wraps the line
}
}
int main()
{
int arr[3][5]={
{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}}; // Create a 3 That's ok 5 Two dimensional array of columns
print(arr,3,5); // Pass the address of the first element of the two-dimensional array and the number of rows and columns .
return 0;
}Dereference notes : The dereference first finds the address of the row of the two-dimensional array, and then adds j To traverse the address of the column and dereference the address of the two-dimensional array column will traverse the two-dimensional array. This dereference writing method is difficult for novices to understand, so there are other writing methods .
#include<stdio.h>
void print(int(*p)[5],int x,int y)
{
int i=0;
for(i=0;i<x;i++)
{
int j=0;
for(j=0;j<y;j++)
{
printf("%d ",p[i][j]);
}
printf("\n"); // Inner loop traverses the array once and then wraps the line
}
}
int main()
{
int arr[3][5]={
{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}}; // Create a 3 That's ok 5 Two dimensional array of columns
print(arr,3,5); // Pass the address of the first element of the two-dimensional array and the number of rows and columns .
return 0;
}Dereference notes : Because it is to traverse a two-dimensional array , The first element address is used for parameter transmission , So it can be written in the form of traversal array , As for why we can traverse like this , This involves knowledge of the underlying assembly code , I won't give you a detailed answer here .
边栏推荐
- Player actual combat 21 audio and video synchronization
- Brush one question every day /537 Complex multiplication
- 如何使用android studio制作一个阿里云物联网APP
- How to set, reset and reverse bit
- Recursive summary of learning function
- Reverse analysis from x86 to x64tips
- What is the default gateway
- Player practice 19 xaudio turn on audio
- Create a slice slice pit using the make method
- G++ error in compiling Win32 program: undefined reference to '__ imp_ GetStockObject‘
猜你喜欢

程序分析与优化 - 6 循环优化

Mold and remainder

2022版Redis数据删除策略

Player actual combat 22 to solve the problems of flower screen and Caton

Llvm pass-- virtual function protection

CSDN博客积分规则

Player practice 17 xvideowidget

Postgresql14 installation and use tutorial

Player practice 20 audio thread and video thread

公司运营中更注重转化的出价策略,如何实现? —Google sem
随机推荐
Mold and remainder
Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
华为设备配置H虚拟专用网
[MySQL] basic database operation
Player actual combat 25 unpacking module add close
什么是自动出价?它的优势是什么?
Details of bypassing safeseh mechanism by using modules that do not enable safeseh
Unhandled exception stack overflow
Wait function in SystemC
Running phase of SystemC
In C language, the main function calls another function, which is understood by assembly code
How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem
Why do Chinese programmers change jobs?
PMP敏捷知识点
Sizeof calculation space size summary
Bridging and net
Player practice 20 unpacking thread
Postgresql14 installation and use tutorial
公司运营中更注重转化的出价策略,如何实现? —Google sem
Perfect ending | detailed explanation of the implementation principle of go Distributed Link Tracking