C语言 指针数组-字符指针数组&整型指针数组 char*s[3] int*a[5] 数组指针int(*p)[4]

Posted DQ_CODING

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 指针数组-字符指针数组&整型指针数组 char*s[3] int*a[5] 数组指针int(*p)[4]相关的知识,希望对你有一定的参考价值。

基本介绍

1.指针数组:由n个指向整型元素的指针而组成,里面存放指针
Int *ptr[3];

2.地址:
ptr[i]:元素地址
&ptr[i]:指针地址

图示

代码:

内存布局:

代码

#include<stdio.h>
#include<string.h>
//指针数组--int
void pointer(int *arr,int len)
{

	int i, *ptr[10];
	for(i=0;i<len;i++)
	{
		ptr[i]=&arr[i];
		printf("arr[%d]=%d address=%p\\n",i,arr[i],&arr[i]);
		printf("ptr[%d]=%d address=%p self-address=%p\\n",i,*ptr[i],ptr[i],&ptr[i]);
	}
}

//指针数组--char
void cpointer(char* s,int len)
{
	char *c[4];
	int i=0;
	printf("s=%s address=%p self-address=%p\\n",s,s,&s);
	while(i<len)
	{
		c[i]=&s[i];
		printf("c[%d]=%c address=%p self-address=%p\\n",i,*c[i],c[i],&c[i]);
		i++;
	}
}

void cpointer2(char *s,int len)
{
	char *c[4];
	int i;
	printf("s=%s address=%p self-address=%p\\n",s,s,&s);
	for(i=0;i<len;i++)
	{
		c[i]=&s[i];
	}
	char *p=c[0];
	for(i=0;i<len;i++)
	{
		printf("c[%d]=%c address=%p self-address=%p\\n",i,*p,p,&p);
		p++;
	}
}
int main()
{
	int i,arr[10];
	int len=sizeof(arr)/sizeof(int);
	char *s="uiop";
	for(i=0;i<len;i++)
	{
		arr[i]=99;
	}
	printf("int_pointer:\\n");
	pointer(arr,len);
	printf("char_pointer:\\n");
	cpointer(s,strlen(s));
	printf("char_pointer2:\\n");
	cpointer2(s,strlen(s));

	getchar();
	return 0;
}

输出:

int_pointer:
arr[0]=99 address=010FF984
ptr[0]=99 address=010FF984 self-address=010FF854
arr[1]=99 address=010FF988
ptr[1]=99 address=010FF988 self-address=010FF858
arr[2]=99 address=010FF98C
ptr[2]=99 address=010FF98C self-address=010FF85C
arr[3]=99 address=010FF990
ptr[3]=99 address=010FF990 self-address=010FF860
arr[4]=99 address=010FF994
ptr[4]=99 address=010FF994 self-address=010FF864
arr[5]=99 address=010FF998
ptr[5]=99 address=010FF998 self-address=010FF868
arr[6]=99 address=010FF99C
ptr[6]=99 address=010FF99C self-address=010FF86C
arr[7]=99 address=010FF9A0
ptr[7]=99 address=010FF9A0 self-address=010FF870
arr[8]=99 address=010FF9A4
ptr[8]=99 address=010FF9A4 self-address=010FF874
arr[9]=99 address=010FF9A8
ptr[9]=99 address=010FF9A8 self-address=010FF878
char_pointer:
s=uiop address=006B5814 self-address=010FF894
c[0]=u address=006B5814 self-address=010FF878
c[1]=i address=006B5815 self-address=010FF87C
c[2]=o address=006B5816 self-address=010FF880
c[3]=p address=006B5817 self-address=010FF884
char_pointer2:
s=uiop address=006B5814 self-address=010FF894
c[0]=u address=006B5814 self-address=010FF860
c[1]=i address=006B5815 self-address=010FF860
c[2]=o address=006B5816 self-address=010FF860
c[3]=p address=006B5817 self-address=010FF860

字符数组指针

具体分析

#include<stdio.h>
#include<string.h>
int main()
{
	char *p[]={"水浒传","三国演义","西游记","红楼梦"};
	int i=0;
	printf("方法1:\\n");
	for(i=0;i<4;i++)
	{
		/*字符指针
	    char *pstr="good dog ww";
	    printf("字符指针指向的字符串内容:%s\\n",pstr);
		所以这里的%s-->p[i]
		数组:地址传递(指针)
		%c *pstr是取出一个字符

		这里不能*p[i],p[i]本身就是一个字符数组指针,无法单独取出一个字符
		*/
//p[0]="水浒传" %s p[0]就直接是取出字符串
		printf("p[%d]=%s address=%p self-address=%p\\n",i,p[i],p[i],&p[i]);
	}


	printf("方法2:\\n");
	char *ptr=p[0];
	i=0;
	while(i<=3)
	{
		printf("p=%s address=%p self-address=%p\\n",ptr,ptr,&ptr);
		i++;
		ptr=p[i];//每一个p[i]的字符内容不同,导致其间隔不同,所以不能使用ptr++
		
	}
	getchar();
	return 0;
}

整型数组指针

#include<stdio.h>
#include<string.h>
//整型指针
int main()
{
	int a=1,b=2,c=4,d=90;
	int*a1=&a,*a2=&b,*a3=&c,*a4=&d;
	int *arr[]={a1,a2,a3,a4};
	int i;
	printf("方法1:\\n");
	for(i=0;i<4;i++)
	{
		//arr[0]=a1=&a=1的地址--%d arr[i]只会取到地址
		//*arr[i]才能取到值
		printf("arr[%d]=%d address=%p self-address=%p\\n",i,*arr[i],arr[i],&arr[i]);
	}
	printf("方法2:\\n");
	i=0;
	int *p=arr[0];
	while(i<=3)
	{
		printf("arr[%d]=%d address=%p self-address=%p\\n",i,*p,p,&p);
		i++;
		p=arr[i];//p++不行 随机存储的,+1不是下一个元素的地址
	}
	getchar();
	return 0;
}

区分

指针数组:
由n个指向整型元素的指针而组成,里面存放指针
Int *ptr[3];

数组指针:
指向一个有n个元素的数组的指针,里面存放的是整型变量(int类型长度为n的数组的首地址),存的是一个数组地址,而不是单个元素
int(*p)[n]
代码详解

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//指针使用 int(*p)[n]与int *p[n]的区别
int main()
{
	int i;
	// int(*p)[n]--指向一个有n个元素的数组的指针,里面存放的是整型变量(int类型长度为n的数组的首地址)
	//存的是一个数组地址,而不是单个元素
	int a[3]={1,2,3};
	int b[3][3]={{1,4,0},{2,5,6},{7,6,8}};
	int(*arr)[3]=b;
	
	printf("int(*arr)[n]:\\n");
	for(i=0;i<3;i++)
	{
		printf("arr[%d]=%d address=%p self-address=%p\\n",i,*arr[i],arr[i],&arr[i]);
	
	}

	//int *p[n]--指针数组,由n个指向整型元素的指针而组成,里面存放指针
	int *p[3];

	for(i=0;i<3;i++)
	{
		p[i]=&a[i];
	}
	printf("int*p[n]:\\n");
	for(i=0;i<3;i++)
	{
		//数组
		printf("arr[%d]=%d address=%p self-address=%p\\n",i,*p[i],p[i],&p[i]);
		
	}
	getchar();
	return 0;
}

以上是关于C语言 指针数组-字符指针数组&整型指针数组 char*s[3] int*a[5] 数组指针int(*p)[4]的主要内容,如果未能解决你的问题,请参考以下文章

C语言进阶笔记深入了解进阶指针

C语言进阶笔记深入了解进阶指针

C语言进阶笔记深入了解进阶指针

C语言指针进阶第二站:指针数组!

C语言如何定义指针指向字符型二维数组

C语言指针进阶第三站,数组指针!