将指针传递给typedef'ed数组

时间:2021-10-14 21:31:53

After initializing a 4x4 array of unsigned chars on the stack, I would like to pass a pointer to the array to another function. I do not understand why this is failing.

初始化堆栈上的4x4无符号字符数组后,我想将指向数组的指针传递给另一个函数。我不明白为什么会失败。

From my point of view, it should be the same as passing a pointer to any other array (the address of where the array is starting).

从我的角度来看,它应该与将指针传递给任何其他数组(数组的起始地址)相同。

Though when trying to pass it, it seems only the first place in the array is at the correct location, and I access random memory on all other places.

虽然在尝试传递它时,似乎只有数组中的第一个位置在正确的位置,并且我在所有其他位置访问随机存储器。

What am I doing wrong here? Any explanation why this is?

我在这做错了什么?任何解释为什么会这样?

#include <stdio.h>

typedef unsigned char multi_array[4][4];


void print_array(multi_array* arr){
    unsigned char i,j;

    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            printf("%d ", *arr[i][j]);
        }
    }
    printf("\n");
}

int main() {
    unsigned char i,j,z;
    multi_array arr;    // Aren't we allocating memory on the stack for a 4x4 u_char array?

    z = 0;

    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            arr[i][j] = z++;
        }
    }

    print_array(&arr); // Arent we passing the address of the beginning of the array here?
    return 0;
}

OUTPUT: 0 4 8 12 48 255 0 0 48 55 23 108 0 0 56 255

输出:0 4 8 12 48 255 0 0 48 55 23 108 0 0 56 255

2 个解决方案

#1


6  

Hiding arrays or pointers behind a typedef is bad practice. You just found out one of many reasons why.

隐藏typedef后面的数组或指针是不好的做法。你刚刚发现了其中一个原因。

multi_array* arr in your example is equivalent to unsigned char(*arr)[4][4], an array pointer. You cannot de-reference it through *arr[i][j] because of operator precedence. [] having higher precedence than *. You would have to do (*arr)[i][j] instead.

你的例子中的multi_array * arr等同于unsigned char(* arr)[4] [4],一个数组指针。由于运算符优先级,您无法通过* arr [i] [j]取消引用它。 []的优先级高于*。你不得不这样做(* arr)[i] [j]。

The proper solution is to not use a typedef at all, but instead use this:

正确的解决方案是根本不使用typedef,而是使用它:

void print_array (size_t x, size_t y, unsigned char arr[x][y]) {
    for(size_t i = 0; i < x; i++){
        for(size_t j = 0; j < y; j++){
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

#2


2  

In statement printf("%d ", *arr[i][j]); operator [] has higher precedence than *. This means that operations are performed in wrong order.

在语句printf(“%d”,* arr [i] [j]); operator []的优先级高于*。这意味着操作以错误的顺序执行。

Use (*arr)[i][j] instead.

请改用(* arr)[i] [j]。

As a general rule, hiding pointers or arrays behind typedef is a bad practice, because it's not obvious what the type is. If you want to use typedef, wrap it in struct definition.

作为一般规则,将指针或数组隐藏在typedef后面是一种不好的做法,因为它的类型并不明显。如果要使用typedef,请将其包装在struct定义中。

#1


6  

Hiding arrays or pointers behind a typedef is bad practice. You just found out one of many reasons why.

隐藏typedef后面的数组或指针是不好的做法。你刚刚发现了其中一个原因。

multi_array* arr in your example is equivalent to unsigned char(*arr)[4][4], an array pointer. You cannot de-reference it through *arr[i][j] because of operator precedence. [] having higher precedence than *. You would have to do (*arr)[i][j] instead.

你的例子中的multi_array * arr等同于unsigned char(* arr)[4] [4],一个数组指针。由于运算符优先级,您无法通过* arr [i] [j]取消引用它。 []的优先级高于*。你不得不这样做(* arr)[i] [j]。

The proper solution is to not use a typedef at all, but instead use this:

正确的解决方案是根本不使用typedef,而是使用它:

void print_array (size_t x, size_t y, unsigned char arr[x][y]) {
    for(size_t i = 0; i < x; i++){
        for(size_t j = 0; j < y; j++){
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

#2


2  

In statement printf("%d ", *arr[i][j]); operator [] has higher precedence than *. This means that operations are performed in wrong order.

在语句printf(“%d”,* arr [i] [j]); operator []的优先级高于*。这意味着操作以错误的顺序执行。

Use (*arr)[i][j] instead.

请改用(* arr)[i] [j]。

As a general rule, hiding pointers or arrays behind typedef is a bad practice, because it's not obvious what the type is. If you want to use typedef, wrap it in struct definition.

作为一般规则,将指针或数组隐藏在typedef后面是一种不好的做法,因为它的类型并不明显。如果要使用typedef,请将其包装在struct定义中。