69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define ARRAYLEN 5
typedef char Type;
void printAddr(Type* arr, int len);
void printAddr2d(Type** arr2d, int rows, int cols);
int main()
{
Type arr[ARRAYLEN];
Type arr2d[ARRAYLEN/2+1][ARRAYLEN];
printf("The address of the array: \n");
printAddr(arr, ARRAYLEN);
printf("-------------------------------\n");
printf("The address of 2-d array: \n");
printAddr2d((Type**)arr2d, ARRAYLEN/2+1, ARRAYLEN);
printf("-------------------------------\n");
return 0;
}
/*
* 打印一维数组的元素地址
*/
void printAddr(Type* arr, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("0x%x%c", (unsigned)(arr+i), (i == len-1) ? '\n': ' ');
}
}
/*
* 打印二维数组的元素地址
*/
void printAddr2d(Type** arr2d, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
printf("row[%d]: ", i);
for (j = 0; j < cols; j++) {
printf("0x%x ", (unsigned)(*(arr2d+i)+j));
}
printf("\n");
}
}