简体   繁体   English

为什么此代码不起作用?

[英]Why this code doesn't work?

i get a segmentation here. 我在这里得到了细分。 However if i declare array as int** and use malloc, it works fine. 但是,如果我将数组声明为int **并使用malloc,则可以正常工作。

#include <stdio.h>

void display(int **p,int numRows,int numCols) //Second Method//
{
    printf("\n");
    int i,j;
    for (i = 0; i< numRows;i++)
    {

        for (j = 0;j< numCols;j++)
        {
            printf("%d\t",p[i][j]);
        }
        printf("\n");
    }
}

int main() {

    int arr[2][2]={{1,2},{4,5}};
        display(arr,2,2);
}

PS I don't need an alternative way, just tell me why this code doesn't work. 附注:我不需要替代方法,只需告诉我为什么这段代码行不通。

arr is an array of 2 arrays of 2 int s. arr是2个int的2个数组的数组。 When it's used in an expression without being the subject of either the unary & or sizeof operators, it evaluates to a pointer to its first element. 当在表达式中使用它而不是一元&sizeof运算符的主题时,它将求值指向其第一个元素的指针。

This is a "pointer to an array of 2 int ", which is the type int (*)[2] . 这是“指向2 int数组的指针”,其类型为int (*)[2] It is not a pointer to a pointer to int , which is the type int ** . 不是指向类型为int ** int指针的指针。 Furthermore, these types are not compatible, and you can't pass the former to a function expecting the latter. 此外,这些类型不兼容,您不能将前者传递给期望后者的函数。

An int ** must be pointing at something that itself is a pointer. int **必须指向本身就是指针的对象。 The pointer you are passing to display is pointing at an array, not a pointer. 您传递给display的指针指向的是数组,而不是指针。

Please don't neglect warnings 请不要忽视警告

expected int **' but argument is of type 'int (*)[2]

arr is an array of 2 arrays of 2 integers arr是2个2的整数的数组

Use : 采用 :

void display(int p[][2],int numRows,int numCols)

An int** is a pointer to a pointer, while an int [2][2] contiguously stores two arrays. 一个int**是指向指针的指针,而一个int [2][2]连续存储两个数组。 That's your mistake. 那是你的错

With int x[2][2] , when you do x[1] , you tell the compiler to access memory by skipping one set of two elements, and the resulting expression is an array of two elements. 使用int x[2][2] ,当您执行x[1] ,您告诉编译器通过跳过两个元素的集合来访问内存,并且结果表达式是两个元素的数组。 With int** x , when you do x[1] , you tell the compiler to access memory by skipping one pointer . 使用int** x时,执行x[1] ,您将跳过一个指针来告诉编译器访问内存。 While you access data the same way for both, the two are simply not laid out the same way. 当您以相同的方式访问两者的数据时,两者的布局方式根本不同。

In memory, x[2][2] = {{1, 2}, {4, 5}} looks like this: 在内存中, x[2][2] = {{1, 2}, {4, 5}}看起来像这样:

// 4 contiguous elements
01 00 00 00  02 00 00 00    04 00 00 00  05 00 00 00

While int** x looks like this: 虽然int** x看起来像这样:

// pointers to integers
aa bb cc dd  aa ab ac ad
// At [aa bb cc dd]:
    01 00 00 00  02 00 00 00
// At [aa ab ac ad]:
    04 00 00 00  05 00 00 00

Therefore, to adapt for an int** , you must create an array of int* pointers dynamically and set each entry to the lower dimension of your array. 因此,要适应int** ,必须动态创建一个int*指针数组,并将每个条目设置为数组的较低维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM