IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 一级指针、二级指针,或是多级指针访问 -> 正文阅读

[C++知识库]一级指针、二级指针,或是多级指针访问

简述

代码测试地址:

  • 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;
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-03-13 21:33:09  更:2022-03-13 21:35:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 4:19:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码