当前位置:网站首页>The ever-changing pointer ----- C language
The ever-changing pointer ----- C language
2022-07-28 19:07:00 【hania_ w】
List of articles
1、 Character pointer
1、 Brief introduction
Before writing pointer , Let's first recall the knowledge of the primary pointer .
1. A pointer is a variable , It's used to store the address , Address unique identification one
Block memory space .
2. The size of the pointer is fixed 4/8 Bytes (32 Bit platform /64 position
platform ).
3. Pointers are typed , The type of pointer determines the type of pointer +- The integer
step , The permission of pointer dereference operation .

This is the most classic use of character pointers , Change the value of a variable by address .
Look at another usage 
Print string by address , Instead of putting the entire string address into , Instead, put the address of the first element of the string into the character pointer , Find the whole string through the first element , To print the entire string .
2、 Example explanation
Let's look at a simple topic
What is the result of running the following code ?
The answer is :
First of all to see str1 And str2, They are all arrays , You need to open up a piece of memory first , To store data . and str3 And str4 It stores the same string “hello world” First element h The address of , therefore str3 And str4 equal 
2、 Pointer array and array pointer
1、 What is pointer array
int arr[ ]: An integer array is an array that holds the integer ;
char arr[ ]: Character array is an array of characters ;
therefore , Pointer array is an array of pointers ;


2、 What is an array pointer
An array pointer is a pointer to an array , It's essentially a pointer , Its shape is like int (*p2)[ ],
p2 It's an array pointer ,p2 Can point to an array , In the array 10 Types are int The elements of . Of course , If you want to know more about array pointers , We must first figure out the array name and & Differences between array names .
3、 Array names and & Array name
In general , The array name represents the address of the first element , But there are also two special cases .
1、sizeof( Array name ): At this point, the array name represents the address of the entire element .
2、& Array name : here , The array name represents the address of the whole array , So what we take out is the address of the whole array .
From this picture, we can't see the difference between the three , Only know that the three points to the same address .
Next , Look at the next picture 
From this picture, we can clearly see the difference between the three ,

Be careful : Unit is byte .
Next, return to the focus , Array pointer and pointer array
Shaping pointer is used to store shaping pointer
Character pointer is a pointer used to store characters
Array pointers are pointers used to store arrays 
Be careful :
4、 The use of array pointers
Unconventional use ( Not recommended )
Print 2D array , Common method 
Let's think about a problem ,arr Is the address of the first element of the array , So two-dimensional arrays arr What is the address of the first element of ? Two dimensional array arr The address of the first element is the address of the first line , therefore , What we pass the parameters is the address of the first row of the array
void print2(int(*p)[5], int r, int c)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%d ", *(*(p + i) + j));
//p It points to the address of the first row of the array ,
//*(p+i) It points to an array arr The first i The address of the line
//*(*(p+i)+j) It points to an array arr Of the i Xing di j The elements of the column
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {
1,2,3,4,5,3,4,5,6,7,4,5,6,7,8 };
//print1(arr, 3, 5);
print2(arr, 3, 5);
// Pass the address of the first row of the array ,
// So we need to use array pointer to receive
}

Let's review the meaning of the following code
int arr[5]; //arr It's an integer array
int *parr1[10]; //parr1 Is an array of integer pointers
int (*parr2)[10]; //parr2 It's an array pointer
int (*parr3[10])[5]; //parr3 Is an array that holds array pointers
4、 Array parameters 、 Pointer parameter
1、 Two dimensional array parameters
void test(int* arr)// error
// Parameters should be received with an array pointer
void test(int *arr[5])//error
//int *arr[5] It's an array of Pointers , It's not a pointer
void test(int(*arr)[5])//ok
// The array pointer is correct
void test(int** arr)//error
// The secondary pointer is a variable that puts the address of the primary pointer , Cannot receive the address of a line of elements
int main()
{
int arr[3][5] = {
0 };
test(arr);// Pass the address of the first row of the array
return 0;
}
2、 First level pointer parameter transfer
void print(int* p)
int main()
{
// When my parameter part is a pointer , We can pass the reference in this way
int a = 10;
int* ptr = &a;
int arr[10];
print(&a);
print(ptr);
print(arr);
}
3、 The secondary pointer transmits parameters
// If the formal parameter of the function is a secondary pointer .
// The calling function can pass parameters like this
void test(int** p)
int main()
{
int* p1;
int** p2;
int* arr[10];
test(&p1);
test(p2);
test(arr);
}
5、 A function pointer
1、 Function pointer Introduction
Array pointer : Pointer to array ;
A function pointer : Pointer to function ;
As can be seen from the above figure ,& What the function name takes out is indeed the address of the function
For functions ,& Function name and function name get the address of the function
The function pointer is shaped like :
int Add(int x, int y)
{
return (x + y);
}
int main()
{
int (*pf)(int, int) = &Add;// A function pointer
// When we want to call the function pointer
int ret = (*pf)(2,3);
// You can also write like this int ret = pf(2,3);
// You can also write like this int ret = Add(2,3);
printf("%d",ret);// 5
}
2、 Use function pointer to realize simple calculator
Let's first look at how to implement a calculator in a common way
// First write out each function
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf("*************************\n");
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
printf(" Enter the operands :");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("ret = %d\n", ret);
break;
case 2:
printf(" Enter the operands :");
scanf("%d %d", &x, &y);
ret = sub(x, y);
printf("ret = %d\n", ret);
break;
case 3:
printf(" Enter the operands :");
scanf("%d %d", &x, &y);
ret = mul(x, y);
printf("ret = %d\n", ret);
break;
case 4:
printf(" Enter the operands :");
scanf("%d %d", &x, &y);
ret = div(x, y);
printf("ret = %d\n", ret);
break;
case 0:
printf(" Exit procedure \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
Next, use function pointer to realize simple calculator
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
void Calc(int(*pf)(int x, int y))
// No matter what function comes up , We use function pointer to receive
{
int x, y;
int ret = 0;
printf(" Please enter two operands :>\n");
scanf("%d %d", &x, &y);
ret = pf(x, y);
printf("%d\n", ret);
}
int main()
{
int input = 0;
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf("*************************\n");
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
Calc(add);
break;
case 2:
Calc(sub);
break;
case 3:
Calc(mul);
break;
case 4:
Calc(div);
break;
case 0:
printf(" Exit procedure \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
6、 Pointer array topic resolution
Look directly at the code
int main()
{
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(&a + 1));//4/8
//&a What we get is the address of the array
//&a--> int(*)[4]
//&a+1 Is from an array a The address of skipped a backward (4 Of an integer element ) Size of array
//&a+1 Or the address , Yes, the address is 4/8 byte
//
printf("%d\n", sizeof(&a[0]));//4/8
//&a[0] It's the address of the first element
// It calculates the size of the address
printf("%d\n", sizeof(&a[0] + 1));//4/8
//&a[0]+1 Is the address of the second element
// Size is 4/8 Bytes
//&a[0]+1 --> &a[1]
//
printf("%d\n", sizeof(a));//16
//sizeof( Array name ), The array name represents the entire array , It calculates the size of the entire array , Unit is byte
printf("%d\n", sizeof(a + 0));//4
//a Not alone sizeof Inside , No address , therefore a Is the address of the first element ,a+0 Or the address of the first element
// It's the address , Size is 4/8 Bytes
printf("%d\n", sizeof(*a));//4
//*a Medium a Is the address of the first element of the array ,*a Is to dereference the address of the first element , What you find is the first element
// The size of the first element is 4 Bytes
printf("%d\n", sizeof(a + 1));
// there a Is the address of the first element of the array
//a+1 Is the address of the second element
//sizeof(a+1) Is the size of the address
printf("%d\n", sizeof(a[1]));//4
// It calculates the size of the second element
printf("%d\n", sizeof(&a));//4/8
//&a The address of the extracted array , Address of array , It's just an address
printf("%d\n", sizeof(*&a));//16
//&a----> int(*)[4]
//&a What you get is the address of the array name , The type is int(*)[4], Is an array pointer
// Array pointer dereference finds an array
//*&a ---> a
//
//& and * Offset
//*&a ---> a
return 0;
}
int main()
{
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));//6
//sizeof( Array name )
printf("%d\n", sizeof(arr + 0));//4/8
//arr + 0 Is the address of the first element of the array
printf("%d\n", sizeof(*arr));//1
//*arr Is the first element of the array , Size is 1 byte
//*arr --> arr[0]
//*(arr+0) --> arr[0]
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//4/8
//&arr Is the address of the array , Yes, the address is 4/8 Bytes
printf("%d\n", sizeof(&arr + 1));//4/8
//&arr + 1 Is the address after the array
//
printf("%d\n", sizeof(&arr[0] + 1));//4/8
//&arr[0] + 1 Is the address of the second element
//
return 0;
}
int main()
{
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", strlen(arr));// Random value
printf("%d\n", strlen(arr + 0));// Random value
// printf("%d\n", strlen(*arr));//--> strlen('a');-->strlen(97);// Wild pointer
// printf("%d\n", strlen(arr[1]));//-->strlen('b')-->strlen(98);
printf("%d\n", strlen(&arr));// Random value
printf("%d\n", strlen(&arr + 1));// Random value -6
printf("%d\n", strlen(&arr[0] + 1));// Random value -1
return 0;
}
int main()
{
//char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
char arr[] = "abcdef";
//strlen Is to find the length of the string , Focus on... In the string \0, The calculation is \0 The number of characters that appear before
//strlen It's a library function , For strings only
//sizeof Only pay attention to the size of memory space , Don't care what's in the memory
//sizeof It's the operator
//[a b c d e f \0]
printf("%d\n", strlen(arr));//6
printf("%d\n", strlen(arr + 0));//6
//printf("%d\n", strlen(*arr));//err
//printf("%d\n", strlen(arr[1]));//err
printf("%d\n", strlen(&arr));//6
printf("%d\n", strlen(&arr + 1));// Random value
printf("%d\n", strlen(&arr[0] + 1));//5
//[a b c d e f \0]
//printf("%d\n", sizeof(arr));//7
//printf("%d\n", sizeof(arr + 0));//4/8
//printf("%d\n", sizeof(*arr));//1
//printf("%d\n", sizeof(arr[1]));//1
//printf("%d\n", sizeof(&arr));//4/8
//printf("%d\n", sizeof(&arr + 1));//4/8
//printf("%d\n", sizeof(&arr[0] + 1));//4/8
return 0;
}
int main()
{
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
a[0] Is the array name of this one-dimensional array in the first line , Put it alone sizeof Inside ,a[0] Represents the first entire one-dimensional array
sizeof(a[0]) Calculate the size of the first row
printf("%d\n", sizeof(a[0] + 1));
a[0] Not alone in sizeof Inside , I didn't take the address ,a[0] It means the address of the first element
Is the address of the first element of the first row of this one-dimensional array ,a[0] + 1 Is the address of the second element in the first line
printf("%d\n", sizeof(*(a[0] + 1)));
a[0] + 1 Is the address of the second element in the first line
*(a[0] + 1)) It's the second element in the first line
printf("%d\n", sizeof(a + 1));//4/8
a Although it is the address of a two-dimensional array , But it is not placed alone sizeof Inside , I didn't take the address
a Represents the address of the first element , The first element of a two-dimensional array is its first row ,a It's the address on the first line
a+1 Just skip the first line , Indicates the address of the second line
printf("%d\n", sizeof(*(a + 1)));//16
*(a + 1) Is the dereference of the address in the second line , I got the second line
*(a+1)-->a[1]
sizeof(*(a+1))-->sizeof(a[1])
printf("%d\n", sizeof(&a[0] + 1));//4/8
&a[0] - Address the array name in the first row , Take out the address on the first line
&a[0]+1 - What you get is the address on the second line
printf("%d\n", sizeof(*(&a[0] + 1)));//16
printf("%d\n", sizeof(*a));//16
a Represents the address of the first element , It's the address on the first line
*a Is the dereference of the address in the first line , What you get is the first line
printf("%d\n", sizeof(a[3]));//16
printf("%d\n", sizeof(a[0]));//16
int a = 10;
sizeof(int);
sizeof(a);
return 0;
}
If there is an error , You are welcome to send a private letter to correct !
边栏推荐
- The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
- LeetCode_ 343_ integer partition
- Implementation of grayscale publishing with haproxy
- 三分钟了解快来新媒体
- Is the software testing industry really saturated?
- Leetcode binary tree class
- 【雷达】基于核聚类实现雷达信号在线分选附matlab代码
- AI has changed thousands of industries. How can developers devote themselves to the new "sound" state of AI voice
- Is it easy to learn the zero foundation of software testing?
- Is zero basic software testing training reliable?
猜你喜欢

Is software testing really as good as online?

Why app uses JSON protocol to interact with server: serialization related knowledge

Win11系统svchost.exe一直在下载怎么办?

jvm四种引用类型

1、 My first wechat applet

我的创作纪念日 -- 2022年7月25日

Getting started with QT & OpenGL

Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.

使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版

GC垃圾回收器详解
随机推荐
Decimal to binary advanced version (can convert negative numbers and boundary values)
112. 使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
C and SQL mixed programming, vs need to download what things
New upgrade! The 2022 white paper on cloud native architecture was released
EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析
How to use the white list function of the video fusion cloud service easycvr platform?
Full analysis of warehouse building on the lake: how to build a lake warehouse integrated data platform | deepnova technology collection series open class phase IV
2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
Is zero basic software testing training reliable?
Haproxy implements proxy configuration
QT - CPP database operation
Two month software testing training scam? How to choose training institutions?
AI 改变千行万业,开发者如何投身 AI 语音新“声”态
redis优势以及数据结构相关知识
Special Lecture 6 tree DP learning experience (long-term update)
The open source of "avoiding disease and avoiding medicine" will not go far
What does real HTAP mean to users and developers?
QT & OpenGL lighting
Is two months of software testing training reliable?