[英]Print dynamic 2-D matrix with a separated function
I can't understand what's happening. 我不明白发生了什么。 I created a matrix following this post: Function to dynamically allocate matrix
我在这篇文章之后创建了一个矩阵: 动态分配矩阵的函数
To print the matrix, I created this function: 为了打印矩阵,我创建了以下函数:
void PrintMatrix(uint8_t *matrix, size_t nrows, size_t ncols)
{
// Prints matrix
size_t i, j;
printf("\n");
for (i = 0; i < nrows; ++i)
{
for (j = 0; j < ncols ; ++j)
{
printf("%" PRIu8 "\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
This function results in a compilation error: 此函数导致编译错误:
error: subscripted value is neither array nor pointer nor vector
printf("%" PRIu8 "\t", matrix[i][j]);
In order to check the function, I copied the code inside main() and ran it, suprisingly without any problem, so, why it doesn't work when run as a separated function? 为了检查该函数,我意外地将代码复制到main()中并运行了它,这没有任何问题,因此,为什么当作为单独的函数运行时它不起作用?
matrix
is of type uint8_t *
. matrix
的类型为uint8_t *
。 It should be of type uint8_t (*)[ncols]
if you are passing the array to the function call (which will convert to pointer to array). 如果将数组传递给函数调用(它将转换为指向数组的指针),则其类型应为
uint8_t (*)[ncols]
。
Change 更改
void PrintMatrix(uint8_t *matrix, size_t nrows, size_t ncols)
to 至
void PrintMatrix(size_t nrows, size_t ncols, unit8_t (*matrix)[ncols])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.