二维数组初始化
int arr[2][3] = {{1, 2, 3},{4, 5, 6}};
int arr2[2][3] = {1, 2, 3, 4, 5, 6};
int arr3[][3] = {{1, 2, 3},{4, 5, 6}};
memset(arr, 0, sizeof(arr)); // 清空二维数组
行指针
int(*p)[3]; //p1是行指针,用于指向数组长度为3的int型数组
double(*p2)[5]; // p2是行指针,用于指向数组长度为3的double型数组
二维数组名是行地址
int arr[3][2] = {{1, 2},{4, 6}, {7,9}};
int(*p)[2] = arr;
二维数组做函数参数
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
// void fun(int p[][2], int len) {
void fun(int(*p)[2], int len) {
for (size_t i = 0; i < len; i++)
{
for (size_t j = 0; j < 2; j++)
{
cout << "p["<<i<<"]"<<"["<<j<<"] ="<<p[i][j] << " ";
}
cout << endl;
}
}
int main() {
int arr[3][2] = {{1, 2},{4, 6}, {7,9}};
fun(arr, 3);
return 0;
}
image.png
网友评论