当前位置:网站首页>第二部分—C语言提高篇_4. 二级指针
第二部分—C语言提高篇_4. 二级指针
2022-07-24 06:13:00 【qq_43205256】
4.1 二级指针基本概念
这里让我们花点时间来看一个例子,揭开这个即将开始的序幕。考虑下面这些声明:
int a = 12;
int *b = &a;它们如下图进行内存分配:

假定我们又有了第3个变量,名叫c,并用下面这条语句对它进行初始化:
c = &b;它在内存中的大概模样大致如下:

问题是:c的类型是什么?显然它是一个指针,但它所指向的是什么?变量b是一个“指向整型的指针”,所以任何指向b的类型必须是指向“指向整型的指针”的指针,更通俗地说,是一个指针的指针。
它合法吗?是的!指针变量和其他变量一样,占据内存中某个特定的位置,所以用&操作符取得它的地址是合法的。
那么这个变量的声明是怎样的声明的呢?
int **c = &b;那么这个**c如何理解呢?*操作符具有从右想做的结合性,所以这个表达式相当于*(*c),我们从里向外逐层求职。*c访问c所指向的位置,我们知道这是变量b.第二个间接访问操作符访问这个位置所指向的地址,也就是变量a.指针的指针并不难懂,只需要留心所有的箭头,如果表达式中出现了间接访问操作符,你就要随箭头访问它所指向的位置。
4.2 二级指针做形参输出特性
二级指针做参数的输出特性是指由被调函数分配内存。
//1.被调函数,由参数n确定分配多少个元素内存
void allocate_space(int **arr,int n)
{
//堆上分配n个int类型元素内存
int *temp = (int *)malloc(sizeof(int)* n);
if (NULL == temp)
{
return;
}
//给内存初始化值
int *pTemp = temp;
for (int i = 0; i < n;i ++)
{
*pTemp = i;
pTemp++;
}
//指针间接赋值
*arr = temp;
}
//2.打印数组
void print_array(int *arr,int n)
{
for (int i = 0; i < n;i ++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
//3.二级指针输出特性(由被调函数分配内存)
void test()
{
int *arr = NULL;
int n = 10;
//给arr指针间接赋值
allocate_space(&arr,n);
//输出arr指向数组的内存
print_array(arr, n);
//释放arr所指向内存空间的值
if (arr != NULL)
{
free(arr);
arr = NULL;
}
}输出结果
0 1 2 3 4 5 6 7 8 94.3 二级指针做形参输入特性
二级指针做形参输入特性是指由主调函数分配内存。
//打印数组
void print_array(int **arr,int n)
{
for (int i = 0; i < n;i ++)
{
printf("%d ",*(arr[i]));
}
printf("\n");
}
//二级指针输入特性(由主调函数分配内存)
void test()
{
int a1 = 10;
int a2 = 20;
int a3 = 30;
int a4 = 40;
int a5 = 50;
int n = 5;
int** arr = (int **)malloc(sizeof(int *) * n);
arr[0] = &a1;
arr[1] = &a2;
arr[2] = &a3;
arr[3] = &a4;
arr[4] = &a5;
print_array(arr,n);
free(arr);
arr = NULL;
}输出结果
10 20 30 40 50边栏推荐
- 9. Use grid technology to draw a Pentagon on the screen.
- Jinan renshe has signed 1w+ electronic labor contract, which greatly helps HR digitalization
- Three level classification / menu query tree structure
- yocs_ velocity_ Smooth source code compilation
- Ge port: sgmii mode and SerDes mode
- Redis distributed cache learning notes
- Libevent and multithreading
- Jsonobject is sorted in A-Z order of key
- You are you, and no one can replace you
- 你不可能让每个人都满意!
猜你喜欢
随机推荐
不去和谁比较,只需做好自己
华为专家自述:如何成为优秀的工程师
在线问题反馈模块实战(十二):实现图片删除功能
MGR_ mysqlsh_ Keepalive high availability architecture deployment document
OSS authorizes a single bucket permission
C process running permission
/etc/rc. Local setting UI program startup and self startup
Can you increase muscle without exercise??? Just get an injection of hibernating black bear serum
tensorflow boolean_ Mask function
[learning notes] possible reasons and optimization methods for white screen on Web pages
Redis special data type bitmap
[lvgl (3)]
metaRTC5.0实现君正的纯C的webrtc版IPC
《大厂面试》之JVM篇21问与答
Thinking of data analysis -- analyzing the retail industry as a whole -- an all-round and multifaceted detailed analysis
[C language] operator details (in-depth understanding + sorting and classification)
[lvgl (5)] label usage
[lvgl layout] flexible layout
Wix path with spaces
Sealos packages and deploys kubesphere container platform







![[lvgl (5)] label usage](/img/55/f25a510cf04caff7ee15e72360c3a1.png)
![[lvgl layout] flexible layout](/img/7b/db932e25b13a0b494e7920075cf1e2.png)
