c - 传递一个 "normal"二维数组作为 **

标签 c arrays dynamic-allocation

我想这样做,但行不通。是否可以这样做,或者我是否必须将 A 声明为双指针浮点**?请注意,我需要一个适用于各种数组维度的通用函数,因此我无法更改函数参数。

void func(float** arr, int x, int y){
    //access to arr[0][0] for e.g. causes SEGFAULT
}
...
float A[2][2]={{1,0},{0,1}};
func(A, 2, 2);

最佳答案

要将二维数组传递给函数,您需要在编译时至少知道二维中的元素数。

void func(float arr[][2], int n)

如果您在编译时不知道数组的大小,您无能为力,除了:

void func(float *arr, int n, int m)
{
    // getting arr[i][j] with arr[i*m + j]
}

float A[2][2]={{1,0},{0,1}};
func(&A[0][0], 2, 2);

关于c - 传递一个 "normal"二维数组作为 **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878741/

相关文章:

javascript - 如何使用多个分隔符拆分字符串,这些分隔符在 JavaScript 中仅充当一个分隔符?

iphone - 出现 'Assignment makes integer from pointer without cast' 错误

c - printf 在 c 中的实现,我的函数打印两个额外的空格

c - 警告 :return from incompatible pointer type (enabled by default)

python - 是否没有用于使用多索引 pandas 数据框动态创建列的语法糖?

c++ - 查找动态分配数组的大小

c++ - C++中的对象声明和定义

C:打印以 'a' 开头的每个可能的字符串,递归函数

java.lang.OutOfMemory错误: Java heap space with array of 100 objects

javascript - 在 javascript (laravel) 中获取 php 数组