一级指针二级指针,或是多级指针访问

Posted aron566

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一级指针二级指针,或是多级指针访问相关的知识,希望对你有一定的参考价值。

简述

代码测试地址:

  • https://www.nhooo.com/tool/c/
  • https://rextester.com/
  • https://www.tutorialspoint.com/codingground.htm
  • https://www.codechef.com/ide
#include <stdio.h>
#include <stdint.h>
#include <string.h>

/* 5行8列 */
static int xx[5][8] = 0;

static int *pxx[5] = 

	xx[0],
	xx[1],
	xx[2],
	xx[3],
	xx[4],
;
static int val = 0;

void cpt(int **v, int x, int y)

	val = 0;
	int *p = (int *)v;

	for(int i = 0; i < x; i++)
	
		for(int j = 0; j < y; j++)
		
			*(p + i*y + j) = val++;
			printf("add %p + %d = 0x%08x\\n", p, i*y + j, p + i*y + j);
		
	


void cptx(int **v, int x, int y)

	val = 0;
	for(int i = 0; i < x; i++)
	
		for(int j = 0; j < y; j++)
		
			v[i][j] = val++;
		
	


int main()

	/* 普通直接访问 */
	for(int i = 0; i < 5; i++)
	
		for(int x = 0; x < 8; x++)
		
			xx[i][x] = val++;
			printf("xx[%d][%d] = %d;\\n", i, x, xx[i][x]);
		
	
	printf("addr %p\\n", xx);
	
	/* 二级指针访问,内部转一级指针 */
	memset(xx, 0, sizeof(xx));
	cpt((int **)xx, 5, 8);
	printf("\\n");
	for(int i = 0; i < 5; i++)
	
		for(int x = 0; x < 8; x++)
		
			printf("xx[%d][%d] = %d;\\n", i, x, xx[i][x]);
		
		
	printf("\\n");
	
	/* 二级指针直接访问 */
	memset(xx, 0, sizeof(xx));
	cptx((int **)pxx, 5, 8);
	for(int i = 0; i < 5; i++)
	
		for(int x = 0; x < 8; x++)
		
			printf("xx[%d][%d] = %d;\\n", i, x, xx[i][x]);
		
			
   return 0;

测试输出

$gcc -o main *.c -lm
$main
xx[0][0] = 0;
xx[0][1] = 1;
xx[0][2] = 2;
xx[0][3] = 3;
xx[0][4] = 4;
xx[0][5] = 5;
xx[0][6] = 6;
xx[0][7] = 7;
xx[1][0] = 8;
xx[1][1] = 9;
xx[1][2] = 10;
xx[1][3] = 11;
xx[1][4] = 12;
xx[1][5] = 13;
xx[1][6] = 14;
xx[1][7] = 15;
xx[2][0] = 16;
xx[2][1] = 17;
xx[2][2] = 18;
xx[2][3] = 19;
xx[2][4] = 20;
xx[2][5] = 21;
xx[2][6] = 22;
xx[2][7] = 23;
xx[3][0] = 24;
xx[3][1] = 25;
xx[3][2] = 26;
xx[3][3] = 27;
xx[3][4] = 28;
xx[3][5] = 29;
xx[3][6] = 30;
xx[3][7] = 31;
xx[4][0] = 32;
xx[4][1] = 33;
xx[4][2] = 34;
xx[4][3] = 35;
xx[4][4] = 36;
xx[4][5] = 37;
xx[4][6] = 38;
xx[4][7] = 39;
addr 0x6010c0
add 0x6010c0 + 0 = 0x006010c0
add 0x6010c0 + 1 = 0x006010c4
add 0x6010c0 + 2 = 0x006010c8
add 0x6010c0 + 3 = 0x006010cc
add 0x6010c0 + 4 = 0x006010d0
add 0x6010c0 + 5 = 0x006010d4
add 0x6010c0 + 6 = 0x006010d8
add 0x6010c0 + 7 = 0x006010dc
add 0x6010c0 + 8 = 0x006010e0
add 0x6010c0 + 9 = 0x006010e4
add 0x6010c0 + 10 = 0x006010e8
add 0x6010c0 + 11 = 0x006010ec
add 0x6010c0 + 12 = 0x006010f0
add 0x6010c0 + 13 = 0x006010f4
add 0x6010c0 + 14 = 0x006010f8
add 0x6010c0 + 15 = 0x006010fc
add 0x6010c0 + 16 = 0x00601100
add 0x6010c0 + 17 = 0x00601104
add 0x6010c0 + 18 = 0x00601108
add 0x6010c0 + 19 = 0x0060110c
add 0x6010c0 + 20 = 0x00601110
add 0x6010c0 + 21 = 0x00601114
add 0x6010c0 + 22 = 0x00601118
add 0x6010c0 + 23 = 0x0060111c
add 0x6010c0 + 24 = 0x00601120
add 0x6010c0 + 25 = 0x00601124
add 0x6010c0 + 26 = 0x00601128
add 0x6010c0 + 27 = 0x0060112c
add 0x6010c0 + 28 = 0x00601130
add 0x6010c0 + 29 = 0x00601134
add 0x6010c0 + 30 = 0x00601138
add 0x6010c0 + 31 = 0x0060113c
add 0x6010c0 + 32 = 0x00601140
add 0x6010c0 + 33 = 0x00601144
add 0x6010c0 + 34 = 0x00601148
add 0x6010c0 + 35 = 0x0060114c
add 0x6010c0 + 36 = 0x00601150
add 0x6010c0 + 37 = 0x00601154
add 0x6010c0 + 38 = 0x00601158
add 0x6010c0 + 39 = 0x0060115c

xx[0][0] = 0;
xx[0][1] = 1;
xx[0][2] = 2;
xx[0][3] = 3;
xx[0][4] = 4;
xx[0][5] = 5;
xx[0][6] = 6;
xx[0][7] = 7;
xx[1][0] = 8;
xx[1][1] = 9;
xx[1][2] = 10;
xx[1][3] = 11;
xx[1][4] = 12;
xx[1][5] = 13;
xx[1][6] = 14;
xx[1][7] = 15;
xx[2][0] = 16;
xx[2][1] = 17;
xx[2][2] = 18;
xx[2][3] = 19;
xx[2][4] = 20;
xx[2][5] = 21;
xx[2][6] = 22;
xx[2][7] = 23;
xx[3][0] = 24;
xx[3][1] = 25;
xx[3][2] = 26;
xx[3][3] = 27;
xx[3][4] = 28;
xx[3][5] = 29;
xx[3][6] = 30;
xx[3][7] = 31;
xx[4][0] = 32;
xx[4][1] = 33;
xx[4][2] = 34;
xx[4][3] = 35;
xx[4][4] = 36;
xx[4][5] = 37;
xx[4][6] = 38;
xx[4][7] = 39;

xx[0][0] = 0;
xx[0][1] = 1;
xx[0][2] = 2;
xx[0][3] = 3;
xx[0][4] = 4;
xx[0][5] = 5;
xx[0][6] = 6;
xx[0][7] = 7;
xx[1][0] = 8;
xx[1][1] = 9;
xx[1][2] = 10;
xx[1][3] = 11;
xx[1][4] = 12;
xx[1][5] = 13;
xx[1][6] = 14;
xx[1][7] = 15;
xx[2][0] = 16;
xx[2][1] = 17;
xx[2][2] = 18;
xx[2][3] = 19;
xx[2][4] = 20;
xx[2][5] = 21;
xx[2][6] = 22;
xx[2][7] = 23;
xx[3][0] = 24;
xx[3][1] = 25;
xx[3][2] = 26;
xx[3][3] = 27;
xx[3][4] = 28;
xx[3][5] = 29;
xx[3][6] = 30;
xx[3][7] = 31;
xx[4][0] = 32;
xx[4][1] = 33;
xx[4][2] = 34;
xx[4][3] = 35;
xx[4][4] = 36;
xx[4][5] = 37;
xx[4][6] = 38;
xx[4][7] = 39;

以上是关于一级指针二级指针,或是多级指针访问的主要内容,如果未能解决你的问题,请参考以下文章

李连杰的二级指针,二级指针的用途,多级指针的定义使用,数组与指针的区别,void 类型指针,引用

C基础指针的使用

C基础指针的使用

C 真正理解二级指针

C语言提高内容目录

一级指针,二级指针,指针数组,数组和指针