C/C++--定义二维数组及二维数组的传递

// TestVS2012.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

#define ROW 4
#define COL 4

void printValue(int **pValue, int row, int col)
{
	for (int r = 0; r < row; r++)
	{
		for (int c = 0; c < col; c++)
		{
			std::cout << pValue[r][c] << "\t";
			pValue[r][c] = 10;
		}
		std::cout << std::endl;
	}
}

void printValue(int(&values)[ROW][COL])
{
	for (int r = 0; r < ROW; r++)
	{
		for (int c = 0; c < COL; c++)
		{
			std::cout << values[r][c] << "\t";
		}
		std::cout << std::endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	//定义数组
	int **pRec = new int *[COL];
	for (int i = 0; i < ROW; i++)
	{
		pRec[i] = new int[COL];
	}

	//赋值
	for (int r = 0; r < ROW; r++)
	{
		for (int c = 0; c < COL; c++)
		{
			pRec[r][c] = r;
		}
	}

	//打印
	printValue(pRec, ROW, COL);

	std::cout << std::endl;

	//定义数组并赋值
	int test[ROW][COL] = {
		{1, 2, 3, 4},
		{5, 6, 7, 8},
		{9, 10, 11, 12},
		{13, 14, 15, 16}
	};
	//打印
	printValue(test);

	getchar();
	return 0;
}

你可能感兴趣的:(二维数组,new,二级指针,cc++,二维数组传递)