尝试理解带有二维数组的 C 指针。翻看学生时代的一些旧笔记。
代码:
#include<stdio.h>
#define size 100
int main(){
float x[size][size],a[size],b[size],sum=0,result;
int i,j,M,N;
float *xPtr;
xPtr=&x[0][0];
printf("Please enter the number of students: ");
scanf_s("%d",&M);
printf("Please enter the number of quizzes: ");
scanf_s("%d",&N);
printf("\n");
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("Enter the grade for student # %d in quiz # %d: ",i,j);
scanf_s("%f",&x[i][j]);
}
}
printf("\n\t");
for(j=0;j<N;j++){
printf("\t\tquiz # %d",j);
}
printf("\n");
for(i=0;i<M;i++){
printf("student # %d",i);
for(j=0;j<N;j++){
printf("\t\t%.2f\t",x[i][j]);
}
printf("\n");
}
for(i=0;i<M*N;i++){
printf("\nx[%d][%d]=%0.2f\n",i/N,i%N,*(xPtr+i));
}
return 0;
}
控制台输出(学生数量=3,测验数量=2):
避免此问题的替代方法是更改代码的取消引用部分 来自
for(i=0;i<M*N;i++){
printf("\nx[%d][%d]=%0.2f\n",i/N,i%N,*(xPtr+i));
}
至
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("\nx[%d][%d]=%.2f\n",i,j,*((xPtr+i*size)+j));
}
}
此方法的控制台输出成功(学生数量=3,测验数量=2):
对于第一种解引用方法失败的原因给出的解释如下:
QUESTION: Why are the elements
x[1][0],x[1][1],x[2][0],x[2][1]
coming out to be0.00
?
ANSWER: Because there is a mismatch between
size=100
and the actual # of rows and columns. This problem does not arise with 1D arrays where, if the actual # of elements in the 1D array is less than size, it does not matter since the elements after the actual number of elements are zero and do not count.
我的问题:
我不明白给出的解释(为什么存在不匹配以及为什么使用 1 个数组不会出现问题?)。如果指针指向数组的第一个元素(即
xPtr=&x[0][0];
),那么我们不能每次加一(即xPtr+i
)并逐个元素地步进,因为 2D 数组按顺序存储为 a每一行依次排列的一维数组?我不明白成功方法中解引用的实现,具体来说,包含
size
的作用是什么?在取消引用(*((xPtr+i*size)+j))
,我的直觉是做类似*(*(xPtr+i)+j)
的事情使用两个嵌套的取消引用运算符。那行得通吗?他怎么能在这里只使用一个就逃脱惩罚呢?我猜他正在使用 size 来向前移动行大小,但我不完全确定它是如何一起工作的......
最佳答案
我认为您已经有足够的信息来理解这个概念。您知道二维数组实际上存储在线性地址空间中。
不匹配的原因是您在表行中分配的空间多于您使用的空间。当您尝试获取值时,您应该忽略未使用的空间。这就是为什么修复将当前行乘以行大小,以跳转到您要读取的行的第一个元素。
#include <stdio.h>
#define SIZE 4
void main()
{
char buf[SIZE][SIZE];
int n = 2;
printf("Normal addresses:\r\n");
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
printf("%p\t", &buf[i][j]);
}
printf("\r\n");
}
char* start = &buf[0][0];
printf("Broken way:\n\r");
for(int i = 0; i < n*n; i++)
{
printf("%p\t", start + i);
if((i+1)%n == 0)
{
printf("\r\n");
}
}
printf("Fixed way:\n\r");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("%p\t", start + (i*SIZE)+j);
}
printf("\r\n");
}
}
输出:
Normal addresses:
0x7ffe128dd2e0 0x7ffe128dd2e1 0x7ffe128dd2e2 0x7ffe128dd2e3
0x7ffe128dd2e4 0x7ffe128dd2e5 0x7ffe128dd2e6 0x7ffe128dd2e7
0x7ffe128dd2e8 0x7ffe128dd2e9 0x7ffe128dd2ea 0x7ffe128dd2eb
0x7ffe128dd2ec 0x7ffe128dd2ed 0x7ffe128dd2ee 0x7ffe128dd2ef
Broken way:
0x7ffe128dd2e0 0x7ffe128dd2e1
0x7ffe128dd2e2 0x7ffe128dd2e3
Fixed way:
0x7ffe128dd2e0 0x7ffe128dd2e1
0x7ffe128dd2e4 0x7ffe128dd2e5
关于第二个问题,您可以通过*(*(xPtr+i)+j)
获取元素,但您必须向编译器提示行的长度。请考虑以下示例,编译器可以在指针类型中找到该信息。
#include <stdio.h>
#define SIZE 4
int main()
{
char buf[SIZE][SIZE];
int n = 2;
printf("Normal addresses:\r\n");
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
printf("%p\t", &buf[i][j]);
}
printf("\r\n");
}
char (*start)[SIZE] = &buf[0];
printf("Read:\n\r");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("%p\t", (*(start+i)+j));
}
printf("\r\n");
}
}
关于arrays - 二维数组的取消引用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68110205/