• int* p[10]和(int*) p[10]的区别


    int* p[10]是一个有十个指针的数组,该指针指向整型数据;
    (int*) p[10]是一个指向有十个整型数据数组的指针。指针,指向一个数组,数组里面包含10个元素

    看下面这段代码,就能清晰的理解了:

    **(p+i)这个表示加行数,**p+i表示加列数

    int main()
    {
    	int b[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    	int (*p)[4];
    	int i,j;
    	p = &b[0];
    	printf("%d",**(p+1));
    	//for( i=0;i<3;i++)
    	//{
    	//	for(j=0;j<4;j++)
    	//	printf("%d ",*(*(p+i)+j));
    	//	printf("\n");
    	//}
    	system("pause");
    	//return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出结果:
    5

    二维数组:
    在这里插入图片描述

    #include 
    #include 
    #include 
    int main()
    {
    	int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    	int i,j;
    	for(int i =0;i<3;i++)
    	{
    		for(j=0;j<4;j++)
    		printf("%5d",a[i][j]);
    		printf("%5d",*(*(a+i)+j));
    		printf("%5d",(a[i]+j));
    	}
    	system("pause");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出:
    1 2 3 4 512186300 5 6 7 8 912186316 9 10 11 12-185084198812186332

    结合图片和代码来看,
    a<==>&a[0] <=>a+0<=>a+i行指针(站在队伍的开头,并没有进入这个队伍)
    *(a+i)<=>a[i]<=>&a[i][0]<=>指向了一个数组元素

    对以上指针形式进行修改:

    在这里插入图片描述

    int main()
    {
    	int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    	int i,j;
    	int *pa;
    	pa = a;
    	for(i=0;i<12;i++)
    	{
    		printf("%5d",*(pa+i));
    		if((i+1)%4==0)
    			printf("\n");
    		printf("\n");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    pa=(int*)a这里要强转类型,不然会报错

    #include 
    #include 
    #include
    int main()
    {
    	int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    	int i,j;
    	int *pa;
    	pa = (int *)a;//pa = a+ 0
    	for(i=0;i<3;i++)
    	{
    		for(j=0;j<4;j++)
    			printf("%5d",*(pa+4*i+j));//因为在内存中二维数组是线性存储的,结合图像很好理解了
    		printf("\n");
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出:
    1 2 3 4
    5 6 7 8
    9 10 11 12

    改成一维输出:
    for(i=0;i<12;i++)
    {
    printf(“%5d”,*(pa+i));
    if((i+1)%4==0)
    printf(“\n”);
    }
    输出跟上面的是一样的。

    指针数组:

    在这里插入图片描述

    在这里插入图片描述

    #include 
    #include 
    #include
    int main()
    {
    	int i=1,j=2,k=3,m=4;
    	int *pa[4]={&i,&j,&k,&m};
    	for(int v=0;v<4;v++)
    		printf("%d ",pa[v]);
    	system("pause");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出:d4ed9c d4ed80 d4ed84 d4ed88

    在这里插入图片描述

    指向指针的指针

    在这里插入图片描述
    通过下列的代码理解二级指针的关系:

    #include 
    #include 
    #include
    int main()
    {
    	char *pc[] = {"beijing","nanjing","xian"};
    	int i;
    	char **pe = pc;
    	for(i=0;i<3;i++)
    	{
    		puts(*pe++);
    	}
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    输出:
    beijing
    nanjing
    xian

    #include
    #include "process.h"
    typedef struct person
    {
    	char name[31];
    	int age;
    	char address[101];
    }Person;
    int main(void)
    {
    	Person per[2] =
    	{
    		{"Qian",25,"west street 31"},
    		{"Qian",25,"west street 31"}
    	};
    	Person *p1;
    	char *p2 = per[0].name;
    	printf("per=%u\n",per);
    	printf("&per[0]=%u\n",&per[0]);
    	printf("per[0].name=%u\n",per[0].name);
    	printf("&per=%u\n",&per);
    	system("pause");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    per[0].name出的是地址,只有*per[0].name会出Q的AsCII码,而且只有一个字母没有其他的。

    f(char *s)
    {
    	char *p=s;
    	while(*p!='\0') p++;
    	return (p-s);
    }
    int main()
    {
    	printf("%d\n","ABCDEF");
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出:7100232

    注意:s只是一个地址没有正式进入这个字符串中,如果改成*“ABCDEF”,就会输出A的SCII码

    int main()
    {
    
    	int a[10]={6,7,2,9,1,10,5,8,4,3},*p,*s;
    	s =a;
    	printf("Themax:%d",*s);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:Themax:6

    这个s进入了该一维数组

    int main()
    {
    	BitTree S;
    	printf("请输入第一个节点的数据:");
    	S = CreateLink();
    	ShowXianXu(S);
    	//int a = stronger("123","123456");
    	//printf("%d\n",a);
    	char ch[2][5] = {"693","825"},*p[2];
    	int i,j,s=0;
    	for(i=0;i<2;i++)
    		p[i]=ch[i];
    	for(i=0;i<2;i++)
    		for(j=0;p[i][j]>='0'&&p[i][j]<='9';j++)
    			s=10*s+p[i][j]-'0';
    	printf("%d\n",s);
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    输出:693825

    将字符’0’-’9’转换成数字。
    只需将字符变量减去’0’就行了。
    x=str[i]-’0’; //当字符在0和9之间, 计算其代表的数字
    字符和数字在内存里都是以ASCII码形式存储的。
    减去’0’,其实就是减去字符’0’的ASCII码 而字符’0’的ASCII码是30,所以减去’0’也就是减去30,然后就可以得到字符对应的数字了。
    这个写法有点像在炫技,把p改成ch输出答案都是一样的

  • 相关阅读:
    中移链DDC-SDK技术对接全流程(二)
    [性能优化] 使用 esbuild 为你的构建提速
    【Springboot】动态配置数据源,系统自动辨认服务端与本地端数据源
    (1)安装hadoop之虚拟机准备(配置IP与主机名)
    学习python的第7天,她不再开放她的听歌榜单
    GTK渲染摄像头图像数据
    【JAVA入门】JUnit单元测试、类加载器、反射、注解
    Linux中 cpu负载和cpu利用率的区别
    力扣:152. 乘积最大子数组(Python3)
    RDD和DataFrame和Dataset
  • 原文地址:https://blog.csdn.net/qq_43794560/article/details/127434902