欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c/c++ 指针 归纳一

程序员文章站 2022-10-30 20:00:05
// cpp-test.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" void print_array_elment(...
// cpp-test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

void print_array_elment(int a[3], int size)
{
	/*数组作为形参时,退化为指针,不能通过sizeof获取数组的内存大小 ,获取的是指针的大小,commented by zhaoyulong,2016.8.14*/
	printf("\n %s, sizeof(a):%d\n", __function__, sizeof(a));
	for (int i = 0; i < size; i++)
	{
		printf("a[%d]:%d ", i, a[i]);
	}
	printf("\n %s, end \n", __function__);
}

void print_array_elment_by_pointer(int *a, int size)
{
	/*数组作为形参时,退化为指针,不能通过sizeof获取数组的内存大小 ,获取的是指针的大小commented by zhaoyulong,2016.8.14*/
	printf("\n %s, sizeof(a):%d\n", __function__, sizeof(a));
	for (int i = 0; i < size; i++)
	{
		printf("a[%d]:%d ", i, a[i]);/*指针访问元素不仅有*a形式,该种也可以 ,commented by zhaoyulong,2016.8.14*/
	}
	printf("\n %s, end \n", __function__);
}

int max(int a, int b)
{
	return a > b ? a : b;
}


int main(int argc , char **argv)
{
	int *p3 = null;
	char *p4 = null;
	printf("sizeof(*p3):%d;sizeof(*p4):%d\n", sizeof(*p3), sizeof(*p4));
	printf("sizeof(p3):%d, sizeof(p4):%d \n",sizeof(p3),sizeof(p4));

	int a[5] = { 5,13,99 ,17,555};
	int *p = a;
	
	
	char a1[3] = { 'a','z','1' };
	char *p1 = a1;


	printf("p,a:%x,%x \n", p, a);
	printf("a[1],*(a+1):%d,%d \n", a[1], *(a + 1));

	printf("\n访问数组元素的第一种方式:\n");
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
		printf("a[%d]:%d ", i, a[i]);
		a[i] = i + 1;		
	}

	printf("\n访问数组元素的第二种方式:\n");
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{		
		printf("a[%d]:%d : ", i, *(a + i));/*可知,a指向数组第一个元素 ,commented by zhaoyulong,2016.8.14*/
		*(a + i) = i + 2;
	}

	printf("\n访问数组元素的第三种方式:\n");
	p = a;
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{		
		printf("a[%d]:%d ", i, *(p + i));
		*(p + i) = i + 3;
	}

	printf("\n访问数组元素的第四种方式:\n");
	p = a;
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
		printf("a[%d]:%d : ", i, p[i]);
		p[i] = i + 4;
	}

	printf("\n当前数组的元素值:\n");
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
		printf("a[%d]:%d ", i, a[i]);
	}



	printf("\n----------------------------数组名和指针的不同点----------------------------\n");
	p = a;
	printf("数组sizeof大小,sizeof(a):%d \n", sizeof(a));
	printf("数组sizeof大小,sizeof(p):%d \n", sizeof(p));

	//a的类型int *;&a的类型是int (*)[5]。
	printf("\n(addr) a,&a :0x%x,0x%x\n", a, &a);


	//a的类型int *;&a+1的类型是int (*)[5]。
	/*指针的运算,x86下,a+1,加了4;&a+1,加了12 ,commented by zhaoyulong,2016.8.14*/
	printf("\n(addr) a+1,&a +1:0x%x,0x%x\n", a+1, &a+1);
	//printf("\n(addr) &(a+1):0x%x\n", &(a + 1));/*编译错误,指针的运算,& 要求左值或函数指示符ommented by zhaoyulong,2016.8.14*/

	print_array_elment(a, 5);
	/*数组做实参,只能传入数组名,或数组的某个元素值(数组的某个元素值做实参,和一般的实参又没有什么不同了) ,commented by zhaoyulong,2016.8.14*/
	//print_array_elment(a[5], 5);

	print_array_elment_by_pointer(a, 5);

	printf("\n----------------------------指针与数组的各种结合与区分-----------------------\n");
	int *ptr[3];
	int n = 1;
	ptr[0] = &n;
	printf("(value) ptr, &ptr[0] ,ptr[0], &n:%x,%x,%x,%x\n", ptr, &ptr[0], ptr[0], &n);

	int (*ptr1) [3];//定义指针,ptr1是一个指向3个整形值的数组的指针;并未定义任何数组;
	//错误,ptr1是指向数组的指针
	//ptr1[0] = 1;

	//错误,ptr1并未初始化,以下是野指针操作;(*ptr1)类似是int a[3]数组的数组名a;
	//(*ptr1)[0] = 1;

	//错误,指针类型不能赋给int类型
	//(*ptr1)[0] = &n;

	//sizeof 操作符(operator),返回一个对象或者类型所占的内存字节数。
	/*sizeof(*ptr1)结果等同于对数组int a[3]进行sizeof(a) ,但不能据此说int (*ptr1) [3]定义了一个数组commented by zhaoyulong,2016.8.14*/
	printf("sizeof(*ptr1):%d , sizeof(ptr1)%d\n", sizeof(*ptr1),sizeof(ptr1));

	//报错,ptr1未初始化
	//printf("ptr1:0x%x; ptr1+1:0x%; (*ptr1):0x%x; (*ptr1)+1:0x%x \n", ptr1, ptr1 + 1, (*ptr1), (*ptr1) + 1);

	int a6[3] = { 1 };
	
	//错误,指针类型不匹配
	//ptr1 = a6;

	ptr1 = &a6;
	//前面加12;后面加4;
	printf("ptr1:0x%x; ptr1+1:0x%x; (*ptr1):0x%x; (*ptr1)+1:0x%x \n", ptr1, ptr1 + 1, (*ptr1), (*ptr1) + 1);

	/*定义一个数组元素类型不同的,对比,加深理解 ,commented by zhaoyulong,2016.8.14*/
	char (*ptr2)[3];//ptr2 是一个数组指针;指向形如 char a[3]的数组; 
	printf("sizeof(*ptr2):%d , sizeof(ptr2):%d\n", sizeof(*ptr2), sizeof(ptr2));
	char a7[3] = { 'a' };
	ptr2 = &a7;
	printf("ptr2:0x%x; ptr2+1:0x%x; (*ptr2):0x%x; (*ptr2)+1:0x%x \n", ptr2, ptr2 + 1, (*ptr2), (*ptr2) + 1);

	char a8[4] = { 'b' };
	//报错,指针类型不同;除非指针类型强制转换
	//ptr2 = &a8;

	

	/*再推广一下 ,commented by zhaoyulong,2016.8.14*/
	int * (*ptr3)[3];//定义一个指针ptr3,该指针指向形如int * a[5]的数组 
	printf("sizeof(*ptr3):%d , sizeof(ptr3):%d\n", sizeof(*ptr3), sizeof(ptr3));
	int * a9[3];
	ptr3 = &a9;
	//前加12;后加4;
	printf("ptr3:0x%x; ptr3+1:0x%x; (*ptr3):0x%x; (*ptr3)+1:0x%x \n", ptr3, ptr3 + 1, (*ptr3), (*ptr3) + 1);

	/*定义一个数组元素类型不同的,对比,加深理解 ,commented by zhaoyulong,2016.8.14*/
	char *(*ptr4)[5];
	printf("sizeof(*ptr4):%d , sizeof(ptr4):%d\n", sizeof(*ptr4), sizeof(ptr4));
	char *a10[5];
	ptr4 = &a10;
	//前加20;后加4;
	printf("ptr4:0x%x; ptr4+1:0x%x; (*ptr4):0x%x; (*ptr4)+1:0x%x \n", ptr4, ptr4 + 1, (*ptr4), (*ptr4) + 1);
	

	printf("\n----------------------------字符串和字符数组 -----------------------\n");
	char *p2 = "helloworld!";/*字符串 ,commented by zhaoyulong,2016.8.14*/
	char a2[] = { 'h','e','l','l','o','!' };/*字符数组 ,6个元素,commented by zhaoyulong,2016.8.14*/

	//字符数组a3 a4 a5 中的字符和字符串都相同;不同的是这些字符数组的地址不同;
	char a3[] = { "hello!" };
	char a4[] = "hello!";
	char a5[] = { 'h', 'e' ,'l' ,'l' ,'o', '!' ,'\0' };
	//char a6[] = p2;/*字面字符串可以初始化字符数组,但指向字符串的指针不能初始化字符数组 ,commented by zhaoyulong,2016.8.14*/

	//通过指针不能获得字符串的长度
	printf("sizeof(p2):%d,strlen(p2):%d \n", sizeof p2, strlen(p2));

	//strlen函数不能用于求字符数组中字符串的长度,除非能确保字符数组中存在字符串结束标志'\0'
	printf("sizeof(a2):%d,strlen(a2):%d \n", sizeof a2, strlen(a2));
	printf("sizeof(a3):%d,strlen(a3):%d \n", sizeof a3, strlen(a3));
	printf("sizeof(a4):%d,strlen(a4):%d \n", sizeof a4, strlen(a4));
	printf("sizeof(a5):%d,strlen(a5):%d \n", sizeof a5, strlen(a5));

	printf("\n----------------------------指针的指针(二级指针)-----------------------\n");

	char c = 'b';
	char *pc = &c;
	char **ppc;
	//报错,指针类型不匹配;指针只有在相同类型的变量之间赋值时,才不报错!
	//ppc = &c;
	ppc = &pc;

	printf("&c 0x%x, pc 0x%x, &pc 0x%x,ppc 0x%x,&ppc 0x%x\n",&c,pc,&pc,ppc,&ppc);
	printf("c %c ,*pc %c,**pc %c\n",c,*pc,**ppc);


	printf("\n----------------------------函数的指针(二级指针)-----------------------\n");
	
	int (*pfun) (int a, int b);//定义指向函数的指针pfun,注意与函数int * fun(int a,int b)的区别
	pfun = max;//使pfun指向max函数
	int m1 = 0,m2 =0;
	m1 = pfun(3, 0);
	m2 = pfun(3, 4);
	printf("m1=%d,m2=%d\n", m1,m2);


	printf("\n----------------------------二维数组与指针-----------------------\n");
	int aa[3][4] = { {1,2,3,4},{5,6} };
	printf("a[0][2] %d, *(a[0]+2) %d\n", aa[0][2], *(aa[0] + 2));
	printf("\n访问二维数组元素的第一种方式:\n");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j< 4; j++)
		{
			printf("aa[%d][%d] %d  ", i, j, aa[i][j]);
		}
		printf("\n");
	}

	int (*pp)[4];
	pp = aa;

	printf("\n访问二维数组元素的第二种方式:\n");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("pp[%d][%d] %d  ", i, j, pp[i][j]);
		}
		printf("\n");
	}

	printf("\n访问二维数组元素的第三种方式:\n");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("*(pp[%d]+%d) %d  ", i, j,*(pp[i]+j));
		}
		printf("\n");
	}

	printf("\n访问二维数组元素的第四种方式:\n");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("*(*(pp+%d) + %d)  %d  ", i, j, *(*(pp+i) + j) );
		}
		printf("\n");
	}

	printf("\n访问二维数组元素的第五种方式:\n");
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf( "(*(pp + %d) )[%d]  %d  ", i, j, (*(pp + i) )[j] );
		}
		printf("\n");
	}

	
	
    return 0;
}