【发布时间】:2021-04-07 18:37:40
【问题描述】:
在过去的几天里,我已经熟悉了二维数组以及操作它们所涉及的指针算法
int array[2][2] = {{1,2},{3,4}}, *p = (*a);
// to access the value of the ith element in the array
// you can do *(p+i)
这很简单,但是当访问二维数组中的元素时,内存中的数组布局是线性的,即看起来像这样:
[1] [2] [3] [4]
0x4 0x8 0x12 0x16
我的问题是如何使用这种在 C 中引用和访问数组的方式来操作行和列?
例如我下面有这个程序:
#include <stdio.h>
int main(void){
int a[4][4] = {{1,2,3,4},{5,6,7,8},{1,2,3,4},{5,6,7,333}}, input = 0;
int *p = &(*(*a)), i = 0;
for (;p <= ((*a)+15); p++){
printf("Enter 4 numbers for array %d\n: ", i);
scanf("%d", &input);
*p = input; i++;
}
return 0;
}
我想在数组中一次输入 4 个数字,而不使用括号,只使用指针算法,我怎么知道何时到达数组末尾,然后提示填写下一个数组?以及如何分别处理列和行?
p.s 我已经对此进行了研究,但我在 stackoverflow 中找到的答案并没有充分回答我的问题。
【问题讨论】:
标签: arrays c pointers implicit-conversion pointer-arithmetic