C语言学习之二级指针&指针数组

c 复制代码
//第一种方式:
int a,b,c,d,e;
a = 11;
b = 22;
c = 33;
d = 44;
e = 55;
int *p[5] = {&a,&b,&c,&d,&e};
//第二种方式:
int a,b,c,d,e;
a = 11;
b = 22;
c = 33;
d = 44;
e = 55;
int *p[5] = {NULL};
p[0] = &a;
p[1] = &b;
p[2] = &c;
p[3] = &d;
p[4] = &e;
  • 取指针数组每个元素的值(即普通变量的地址):
c 复制代码
for(int i = 0; i < 5; i++){
	
	     printf("p[%d] = %p\n",i,p[i]);
	
	}
  • 取指针数组每个元素指向的地址空间的值(即普通变量的值):
c 复制代码
for(int j = 0; j < 5; j++){
	
	     printf("*p[%d] = %d\n",j,*p[j]);
	
	
	}
  • 对指针数组的每个元素进行取地址的操作(即每个元素的地址):
c 复制代码
for(int k = 0; k < 5; k++){
	
	     printf("&p[%d] = %p\n",k,&p[k]);
	
	}
  • 输出字符型指针数组中每个元素指向的字符串:
c 复制代码
char *s[5] = {"I","Love","Henan","and","Beijing"};

	for(int i = 0; i < 5; i++){

		printf("%s ",s[i]);
	
	}
  • 输出字符型数组存储的字符串:
c 复制代码
char s1[20] = "hellobeijng";
printf("%s\n",s1);
  • 输出字符型指针指向的字符串:
c 复制代码
char *s2 = "hihenan";
printf("%s\n",s2);
  • 测试代码:
c 复制代码
#include<stdio.h>

int main(int argc, const char *argv[])
{

	int a,b,c,d,e;
	a = 11;
	b = 22;
	c = 33;
	d = 44;
	e = 55;

	int *p[5] = {NULL};

	p[0] = &a;
	p[1] = &b;
	p[2] = &c;
	p[3] = &d;
	p[4] = &e;
    
	//int *p[5] = {&a,&b,&c,&d,&e};
	
	printf("指针数组占用内存空间的大小:%ld\n",sizeof(p));
	printf("指针数组元素的个数:%ld\n",sizeof(p)/sizeof(int *));
    
	printf("a = %d,&a = %p\n",a,&a);
	printf("b = %d,&b = %p\n",b,&b);
	printf("c = %d,&c = %p\n",c,&c);
	printf("d = %d,&d = %p\n",d,&d);
	printf("e = %d,&e = %p\n",e,&e);
	puts("");

	for(int i = 0; i < 5; i++){
	
	     printf("p[%d] = %p\n",i,p[i]);
	
	}
	puts("");

	for(int j = 0; j < 5; j++){
	
	     printf("*p[%d] = %d\n",j,*p[j]);
	
	
	}
	puts("");
	for(int k = 0; k < 5; k++){
	
	     printf("&p[%d] = %p\n",k,&p[k]);
	
	}
	puts("");

	char *s[5] = {"I","Love","Henan","and","Beijing"};

	for(int i = 0; i < 5; i++){


		printf("%s ",s[i]);
	
	
	
	}
	puts("");

	char s1[20] = "hellobeijng";
	printf("%s\n",s1);


	char *s2 = "hihenan";
	printf("%s\n",s2);

	return 0;
}
  • 运行结果:
c 复制代码
指针数组占用内存空间的大小:40
指针数组元素的个数:5
a = 11,&a = 0x7ffcb7f36f84
b = 22,&b = 0x7ffcb7f36f88
c = 33,&c = 0x7ffcb7f36f8c
d = 44,&d = 0x7ffcb7f36f90
e = 55,&e = 0x7ffcb7f36f94

p[0] = 0x7ffcb7f36f84
p[1] = 0x7ffcb7f36f88
p[2] = 0x7ffcb7f36f8c
p[3] = 0x7ffcb7f36f90
p[4] = 0x7ffcb7f36f94

*p[0] = 11
*p[1] = 22
*p[2] = 33
*p[3] = 44
*p[4] = 55

&p[0] = 0x7ffcb7f36fb0
&p[1] = 0x7ffcb7f36fb8
&p[2] = 0x7ffcb7f36fc0
&p[3] = 0x7ffcb7f36fc8
&p[4] = 0x7ffcb7f36fd0

I Love Henan and Beijing 
hellobeijng
hihenan
  • 二级指针&指针数组的关系:
  • 可以使用二级指针指向一个指针数组;
c 复制代码
数据类型*  指针数组名[元素个数];
数据类型*  *二级指针变量名;
  • 两者等价关系:
c 复制代码
指针数组名 <==> 二级指针变量名
  • 测试代码:
c 复制代码
#include<stdio.h>

int main(int argc, const char *argv[])
{
    int a = 11, b = 22, c = 33, d = 44, e = 55;
	int *p[5] = {&a,&b,&c,&d,&e};

	int **q = p;
	
	for(int i = 0; i < 5; i++){

        printf("q[%d] = %p\n",i,q[i]);
		printf("p[%d] = %p\n",i,p[i]);
	    printf("*(q + %d) = %p\n",i,*(q + i));
	    printf("*(p + %d) = %p\n",i,*(p + i));
	
	}
    puts("");

	for(int i = 0; i < 5; i++){


		printf("*q[%d] = %d\n",i,*q[i]);
		printf("*p[%d] = %d\n",i,*p[i]);
		printf("*(*(q + %d)) = %d\n",i,*(*(q + i)));
		printf("*(*(p + %d)) = %d\n",i,*(*(p + i)));
	
	
	
	
	}
    puts("");
    for(int i = 0; i < 5; i++){


		printf("&q[%d] = %p\n",i,&q[i]);
		printf("&p[%d] = %p\n",i,&p[i]);
		printf("q + %d = %p\n",i,q + i);
		printf("p + %d = %p\n",i,p + i);
	
	}


	return 0;
}
  • 运行结果:
c 复制代码
q[0] = 0x7ffd765009a8
p[0] = 0x7ffd765009a8
*(q + 0) = 0x7ffd765009a8
*(p + 0) = 0x7ffd765009a8
q[1] = 0x7ffd765009ac
p[1] = 0x7ffd765009ac
*(q + 1) = 0x7ffd765009ac
*(p + 1) = 0x7ffd765009ac
q[2] = 0x7ffd765009b0
p[2] = 0x7ffd765009b0
*(q + 2) = 0x7ffd765009b0
*(p + 2) = 0x7ffd765009b0
q[3] = 0x7ffd765009b4
p[3] = 0x7ffd765009b4
*(q + 3) = 0x7ffd765009b4
*(p + 3) = 0x7ffd765009b4
q[4] = 0x7ffd765009b8
p[4] = 0x7ffd765009b8
*(q + 4) = 0x7ffd765009b8
*(p + 4) = 0x7ffd765009b8

*q[0] = 11
*p[0] = 11
*(*(q + 0)) = 11
*(*(p + 0)) = 11
*q[1] = 22
*p[1] = 22
*(*(q + 1)) = 22
*(*(p + 1)) = 22
*q[2] = 33
*p[2] = 33
*(*(q + 2)) = 33
*(*(p + 2)) = 33
*q[3] = 44
*p[3] = 44
*(*(q + 3)) = 44
*(*(p + 3)) = 44
*q[4] = 55
*p[4] = 55
*(*(q + 4)) = 55
*(*(p + 4)) = 55

&q[0] = 0x7ffd765009d0
&p[0] = 0x7ffd765009d0
q + 0 = 0x7ffd765009d0
p + 0 = 0x7ffd765009d0
&q[1] = 0x7ffd765009d8
&p[1] = 0x7ffd765009d8
q + 1 = 0x7ffd765009d8
p + 1 = 0x7ffd765009d8
&q[2] = 0x7ffd765009e0
&p[2] = 0x7ffd765009e0
q + 2 = 0x7ffd765009e0
p + 2 = 0x7ffd765009e0
&q[3] = 0x7ffd765009e8
&p[3] = 0x7ffd765009e8
q + 3 = 0x7ffd765009e8
p + 3 = 0x7ffd765009e8
&q[4] = 0x7ffd765009f0
&p[4] = 0x7ffd765009f0
q + 4 = 0x7ffd765009f0
p + 4 = 0x7ffd765009f0
相关推荐
张铁铁是个小胖子34 分钟前
微服务学习
java·学习·微服务
AITIME论道2 小时前
论文解读 | EMNLP2024 一种用于大语言模型版本更新的学习率路径切换训练范式
人工智能·深度学习·学习·机器学习·语言模型
88号技师4 小时前
2024年12月一区SCI-加权平均优化算法Weighted average algorithm-附Matlab免费代码
人工智能·算法·matlab·优化算法
IT猿手4 小时前
多目标应用(一):多目标麋鹿优化算法(MOEHO)求解10个工程应用,提供完整MATLAB代码
开发语言·人工智能·算法·机器学习·matlab
青春男大4 小时前
java栈--数据结构
java·开发语言·数据结构·学习·eclipse
88号技师4 小时前
几款性能优秀的差分进化算法DE(SaDE、JADE,SHADE,LSHADE、LSHADE_SPACMA、LSHADE_EpSin)-附Matlab免费代码
开发语言·人工智能·算法·matlab·优化算法
Zer0_on4 小时前
数据结构栈和队列
c语言·开发语言·数据结构
一只小bit4 小时前
数据结构之栈,队列,树
c语言·开发语言·数据结构·c++
马浩同学5 小时前
【GD32】从零开始学GD32单片机 | DAC数模转换器 + 三角波输出例程
c语言·单片机·嵌入式硬件·mcu
我要学编程(ಥ_ಥ)5 小时前
一文详解“二叉树中的深搜“在算法中的应用
java·数据结构·算法·leetcode·深度优先