当前位置:网站首页>[advanced C language] - Advanced pointer [i]
[advanced C language] - Advanced pointer [i]
2022-07-28 17:45:00 【Crazy orange】
List of articles
1️⃣ Pointer concept
1. The pointer is a Variable , It's used to store the address , A memory space uniquely identified by an address .
2. Pointer size Is constant 4/8 Bytes (32 Bit platform /64 platform ).
3. Pointer is There are types Of , The type of pointer determines the type of pointer + - The range of integers and the permissions of pointer dereference operation .
4. The operation of the pointer .
1. Character pointer
Among the types of pointers, one pointer type is Character pointer char* ;
In general use :
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;// take ch Put the address in pc in
*pc = 'c';
// adopt pc De quote ch The stored data is changed to 'c'
return 0;
}
There's another way to use it :
int main()
{
const char* pstr = "hello bit.";
// Here is to put a string into pstr Is it in the pointer variable ?
printf("%s\n", pstr);// Obviously not ! Output h
return 0;
}
Code :const char* pstr = "hello bit." It's especially easy to think of string hello bit Put it in the character pointer pstr In the , But in essence, the string hello bit. The address of the first character is put in pstr in .
The above code means that the first character of a constant string
hThe address of is stored in the pointer variablepstrin .
So there is such an interview question :
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char* str3 = "hello bit.";
const char* str4 = "hello bit.";
if (str1 == str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if (str3 == str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
The final output here is :
there str3 and str4 Points to the same constant string .C/C++ The constant string is stored in a separate memory area , When a few pointers , To point to the same string , They actually point to the same block of memory , But when initializing different arrays with the same constant string , Different arrays will open up different memory blocks , therefore str1 and str2 Different ,str3 and str3 identical .
2.️ Pointer array
Pointer array is an array of pointers
int* arr1[10];// An array of integer pointers
char* arr2[4];// First level character pointer array
char** arr3[4];// Second level character pointer array
give an example :
int a = 1;
int* pa = &a;
int* arr1[3] = {
pa};
//arr1[0] visit pa The address of
-* ——————————————————*
3.️ Array pointer
3.1 Definition of array pointer
Array pointers are pointers
Shaping the pointerint* paA pointer that can point to shaped data , The array pointer should be : A pointer to an array .
int (*p)[10];
explain :
pThe first and*combination , explainpIs a pointer variable , Then the pointer points to a size of 10 An integer array . thereforepIt's a pointer , Point to an array , It's called array pointer .
Pay attention here :[]Priority is higher than*The no. , So we have to add()To guaranteepThe first and*combination .
3.2 & Array name Array name
For the following array :
int arr[10];
arr It's an array name , The array name indicates the address of the first element of the array .
that &arr What is it? ? Let's look at a piece of code :
#include <stdio.h>
int main()
{
int arr[10] = {
0};
printf("%p\n", arr);
printf("%p\n", &arr);
return 0;
}

thus it can be seen Array name and & The address printed by the array name is the same .
Are the two really the same ?
Let's look at a piece of code :
#include <stdio.h>
int main()
{
int arr[10] = {
0 };
printf("arr = %p\n", arr);
printf("&arr= %p\n", &arr);
printf("arr+1 = %p\n", arr+1);
printf("&arr+1= %p\n", &arr+1);
return 0;
}

According to the above running results, we find : Actually Array name and & Array name , Although the values are the same , But the meaning should be different .
actually :
&arrIt means The address of the entire array , Instead of the address of the first element of the array .( Have a taste of )
In this case&arrThe type is :int(*)[10], It's a kind of Array pointer type .
Address of array +1, Skip the entire array size , therefore&arr+1amount to&arrThe difference is 40.
Why?
&arr+1skip 40 Bytes ?
because :arrThe type isint [10]10 individualintamount to 40 Bytes .
Array oftypeTo determine the +1/ -1 Skip a few bytes .
3.3 The use of array pointers
#include <stdio.h>
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9,0};
int (*p)[10] = &arr;
// Put the array arr To an array pointer variable p
// But we seldom write code like this
return 0;
}
The use of an array pointer : Generally used on two-dimensional arrays
void print1(int (*pa)[5], int r, int c)
//int (*pa)[5] One dimensional array pointer receiving
{
int i = 0;
for (i = 0; i < r; i++)
{
//pa+0 Point to 1 That's ok , + 1 Point to 2 That's ok ...
//+0 Find the first 1 Dimension group ,+1 Find No 2 One dimensional array
int j = 0;
for (j = 0; j < c; j++)
{
//printf("%d ", pa[i][j]);
// Equivalent to the following
printf("%d ", *(*(pa+i)+j));
// (pa+i)——>&arr+i Find No i Line address
//*(pa+i)——>arr[i]
// Find No i Row array name , Equivalent to the first element address
//*(*(pa+i)+j)——>arr[i][j]
// Then, find No i Xing di j Elements
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {
1,2,3,4,5,2,3,4,5,6,3,4,5,6,7 };
print1(arr, 3, 5);
// The address of the first element of the two-dimensional array is the first line
// So the transmission parameter here is
// It is equivalent to passing the address of a one-dimensional array
return 0;
}
You need to know :
int (*p) [5]pThe type is :int(*)[5], Point to an integer array , Array has 5 Elementsp+1skip 5 individualintArray of elements
After learning pointer array and array pointer, let's review and see what the following code means :
int arr[5];//arr Is an array , Array has 5 Elements , Each element is int type
int *parr1[10];//parr It's an array , Array has 10 Elements , Each element is int* type
int (*parr2)[10];//parr2 It's a pointer , Point to one with 10 Array of elements , Each element is int type , therefore parr2 It's an array pointer
int (*parr3[10])[5];//parr3 Is an array , Array has 10 Elements , Each element is int(*)[5],
// The pointer points to a containing 10 Array of elements , therefore parr3 Is an array pointer array
Yes
int (*parr3[10])[5];incomprehension , See the following supplements :

4. Array parameters 、 Pointer parameter
When writing code, it is inevitable to 【 Array 】 perhaps 【 The pointer 】 Pass to function , How to design the parameters of the function ?
4.1 A stack of array parameters
#include <stdio.h>
void test(int arr[])//ok, The size of the array can not be written
{
}
void test(int arr[10])//ok
{
}
void test(int *arr)//ok, Put the shaping address into the shaping pointer
{
}
void test2(int *arr[20])//ok, Array parameters , Formal parameters can be arrays or pointers ,20 You can omit it
{
}
void test2(int **arr)//ok, The secondary pointer receives the primary pointer address
// The array name is equivalent to the address of the first element ,arr2 yes int* Type of address
{
}
int main()
{
int arr[10] = {
0};
int *arr2[20] = {
0};
//20 Elements , Every element int*..
// The array name is equivalent to the address of the first element ,arr2 yes int* Type of address
test(arr);// The array name is equivalent to the address of the first element
test2(arr2);
}
4.2 Two dimensional array parameters
void test(int arr[3][5])//ok, Two dimensional array receiving
{
}
void test(int arr[][])//NO, Two dimensional array of formal parameters , What can be saved , I can't
{
}
void test(int arr[][5])//ok, Save it , Don't save
{
}
void test(int *arr)//NO,arr Is a two-dimensional array ,
// The address of the first element now represents the address of the first line , It is equivalent to a one-dimensional array
// A one-dimensional array cannot be put into a first-order pointer
{
}
void test(int* arr[5])//NO
{
}
void test(int (*arr)[5])//ok, Pointer to 5 Elements , Each element is int
{
}
void test(int **arr)//NO, The secondary pointer can only receive the address of the variable of the primary pointer
{
}
int main()
{
int arr[3][5] = {
0};
test(arr);
// Array parameters are generally written as -- Array name
// Unless you pass an element ,——>&arr[1],arr[1]
}
summary :
1 . Two dimensional array parameters , Function parameter design can only omit the first [ ] The number of .
Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row .
So it's easy to calculate .
2 . Array name of two-dimensional array , Represents the address of the first element , It's actually the address on the first line , The first line is a one-dimensional array
3. The secondary pointer can only receive the address of the primary pointer variable
4.3 First level pointer parameter transfer
#include <stdio.h>
void print(int *p, int sz)
{
int i = 0;
for(i=0; i<sz; i++)
{
printf("%d\n", *(p+i));//*(p+i)==p[i]
//(p+i), If i=1,
//+i Skip an integer address , And then I'll explain the quotation ,
// You find the corresponding element
}
}
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9};
int *p = arr;//1 Address assigned to p
int sz = sizeof(arr)/sizeof(arr[0]);
// First level pointer p, Pass to function
print(p, sz);
return 0;
}
reflection : When the parameter part of a function is a first-order pointer , What parameters can a function take ?
void test1(int *p)
{
}
//test1 What parameters can a function take ?
int main()
{
int a =10;
test1(&a);//ok
int* pa = &a;
test1(pa);//ok
int arr[5];
test1(arr);//ok
}
As long as it is passed to the function, it is essentially a pointer , Argument and formal parameter types can match , Is no problem .
4.4 The secondary pointer transmits parameters
#include <stdio.h>
void test(int** ptr)
{
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int*p = &n;
int **pp = &p;
test(pp);//ok, Itself is a secondary pointer , Transmit parameters to the secondary pointer to receive
test(&p);//ok, The secondary pointer receives the address of the primary pointer variable
return 0;
}
reflection : When the parameter of the function is a secondary pointer , What parameters can be received ?
void test(char **p)
{
}
int main()
{
char c = 'b';
char*pc = &c;
char**ppc = &pc;
char* arr[10];
test(&pc);//ok, The secondary pointer receives the address of the primary pointer variable
test(ppc);//ok, Itself is a secondary pointer , Transmit parameters to the secondary pointer to receive
test(arr);//Ok?
//ok, Each element is int*, The first element address is int*
return 0;
}
// Third level pointer dereference can also .... Level Four .... Level five ....
If you think the article is good , Remember the praise. + Share ~ If something is wrong , Feel free to comment and point out ~
边栏推荐
猜你喜欢

Jerry ac692x --- matrix keyboard addition

es6 Promise

Interviewer: the actual record of algorithm question brushing.pdf I can't even answer it

Public medical database

怎样将IDEA与码云进行绑定

2021 National Undergraduate data statistics and Analysis Competition

软件测试真有网上说的那么好吗?

Talk about the measurement of "post release problems"

Factor in R

MySQL高级-MVCC(超详细整理)
随机推荐
【p5.js实战】我的自画像
Arya professional web automated test platform
新人如何入门学习软件测试
MySQL高级-MVCC(超详细整理)
培训软件测试能不能就业
软件测试前景如何?该如何进行学习呢?
PCA reports error in eigen (crossprod (t (x), t (x)), symmetric = true): 'x' has infinite value or missing value
R语言 sub()用法
电工学下册自学笔记1.23
leetcode系统性刷题(四)-----哈希表与字符串
R语言画图/绘图/作图2
@RequestMapping详解
【 R语言—基础绘图】
点云处理---二叉树
DOS command Daquan basic command + network common command
Technical aspects passed easily, HR: those with only three years of experience in large factories are not worth 20K
【C语言必看】哟写BUG呢,我敢保证你踩过坑
leetcode系统性刷题(一)-----链表、栈、队列、堆
软件测试零基础小白学习需要掌握哪些技能?
Solve package is not available (for R ve [package 'xxx' is not available (for R version x.y.z) "warning?]