简体   繁体   English

在 C 中将多维数组作为参数传递

[英]Passing a multidimensional array as an argument in C

I am trying to initialize matrices in a helper function, but I am getting a warning accessing the matrix inside the helper function that I can't figure out how to fix.我正在尝试在帮助程序 function 中初始化矩阵,但在访问帮助程序 function 中的矩阵时收到警告,我无法弄清楚如何修复。 I was reading about multidimensional arrays and even saw the same notation used to pass and access a matrix in multiple examples, but mine generates a warning and I'm not quite sure why.我正在阅读有关多维 arrays 的内容,甚至在多个示例中看到了用于传递和访问矩阵的相同符号,但我的会产生警告,我不太清楚为什么。

To my knowledge this error means that the argument is not of a type the function is expecting, but I was using it just fine inside the main function before relocating the initialization into its own function.据我所知,此错误意味着该参数不是 function 所期望的类型,但我在主 function 中使用它很好,然后将初始化重新定位到它自己的 ZC1C425268E68385D1AB4ZA507。 This leads me to think that I'm doing something wrong when passing the matrix to the helper function.这让我认为在将矩阵传递给助手 function 时我做错了。

passing argument 1 of 'memmove' makes pointer from integer without a cast [-Wint-conversion] 

Here's my code for the initializer.这是我的初始化程序代码。 p is a pointer to data inside an array that I want to initialize into my matrix. p 是指向要初始化到矩阵中的数组内的数据的指针。 I'm using this type of nested for loop to spread 16 bytes of data coming from p into my matrix 1 byte per cell.我正在使用这种类型的嵌套 for 循环将来自 p 的 16 个字节的数据传播到我的矩阵中,每个单元格 1 个字节。

void initialize(const unsigned char *p, unsigned char (*matrix)[4]) {

   for (unsigned int i = 0; i < 4; i++){
       for (unsigned int j = 0; j < 4; j++){
           memmove(matrix[i][j], p + (4*i+j), 1);    <--- Warning here
       };
   };
};

Initialize is being called in another function like this:在另一个 function 中调用初始化,如下所示:

void func(const unsigned char *p) {
    unsigned char matrix[4][4] = {
        {0x0,0x0,0x0,0x0},
        {0x0,0x0,0x0,0x0},
        {0x0,0x0,0x0,0x0},
        {0x0,0x0,0x0,0x0}
    };
    initialize(p, matrix);
};

Function memmove() takes a pointer as the first argument. Function memmove()将指针作为第一个参数。 While matrix[i][j] is a char , a type from integer family.matrix[i][j]是一个char ,一个来自 integer 系列的类型。 Assingning an integer other than constant 0 to a pointer require a cast.将除常量 0 以外的 integer 分配给指针需要强制转换。 Otherwise a warning is raised.否则会发出警告。

Therefore I expect that in order to copy a single char you should pass a pointer to element matrix[i][j] .因此,我希望为了复制单个char ,您应该将指针传递给元素matrix[i][j] Pointers are formed by applying & operator to objects.通过将&运算符应用于对象来形成指针。

memmove(&matrix[i][j], p + (4*i+j), 1);

however it can written far simpler, more readable and likely more optimal as:然而,它可以写得更简单、更易读并且可能更优化:

matrix[i][j] = p[4 * i + j];

or even by copying the whole array without any loops:甚至通过复制整个数组而没有任何循环:

memmove(matrix, p, 16);

passing argument 1 of 'memmove' makes pointer from integer without a cast [-Wint-conversion]传递 'memmove' 的参数 1 使指针来自 integer 没有强制转换 [-Wint-conversion]

Instead of passing an integer value as the destination:而不是传递 integer作为目标:

unsigned char (*matrix)[4]
...
//         v----------v This is an integer of type unsigned char   
// memmove(matrix[i][j], p + (4*i+j), 1);

Pass the address of the integer as the destination:将 integer 的地址作为目的地传递:

memmove(&matrix[i][j], p + (4*i+j), 1);
//      ^-----------^ This is an address

void *memmove(void *s1, const void *s2, size_t n); expects s1 to be an address.期望s1是一个地址。


matrix in func() is a matrix , aka "2-D array". func()中的matrix一个矩阵,也就是“二维数组”。

matrix in initialize() is not really a matrix , but a pointer to an array[4] of unsigned char . initialize()中的matrix并不是真正的矩阵,而是指向unsigned chararray[4]的指针。


Since C99 and selectively afterwards, code can use a variable length array notation.从 C99 开始以及以后有选择地,代码可以使用可变长度数组表示法。

void initialize(size_t rows, size_t cols, unsigned char (*m)[rows][cols],
    const unsigned char *p) {
  printf("%zu %zu %zu\n", rows, cols, sizeof *m);
  for (size_t r = 0; r < rows; r++){
    for (size_t c = 0; c < cols; c++){
      (*m)[r][c] = *p++;
    }
  }
}


#define ROW 5
#define COL 7
int main(void) {
  unsigned char matrix[ROW][COL];
  unsigned char bytes[sizeof matrix] = "Something abcde fghij klmno pqrst uvwxy z";

  size_t r = sizeof matrix / sizeof matrix[0];
  size_t c = sizeof matrix[0] / sizeof matrix[0][0];
  printf("%zu %zu %zu\n", r, c, sizeof matrix);
  initialize(r, c, &matrix, bytes);
}

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

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