gpt4 book ai didi

arrays - 二维数组解引用问题

转载 作者:行者123 更新时间:2023-12-04 07:28:09 26 4
gpt4 key购买 nike

试图理解带有 2D 数组的 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):output_1
为避免此问题而给出的替代方法是更改​​代码的取消引用部分
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):output_2
关于为什么第一种解引用方法失败的解释如下:

QUESTION: Why are the elements x[1][0],x[1][1],x[2][0],x[2][1] coming out to be 0.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 )并逐个元素地递增,因为二维数组按顺序存储为 aa每一行一个接一个的一维数组?
  • 我不明白成功方法中解引用的实现,具体来说,包含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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com