通过指针传递二维数组

时间:2021-03-09 21:29:27

How do I pass the m matrix to foo()? if I am not allowed to change the code or the prototype of foo()?

如何将m矩阵传递给foo()?如果不允许我更改foo()的代码或原型?

void foo(float **pm)
{
    int i,j;
    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
            printf("%f\n", pm[i][j]);

}

int main ()
{
    float m[4][4];

    int i,j;
    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
            m[i][j] = i+j;

    foo(???m???);
}

9 个解决方案

#1


15  

If you insist on the above declaration of foo, i.e.

如你坚持上述“foo”的声明,即

void foo(float **pm)

and on using a built-in 2D array, i.e.

使用内置的2D数组。

float m[4][4];

then the only way to make your foo work with m is to create an extra "row index" array and pass it instead of m

然后,让你的foo与m一起工作的唯一方法是创建一个额外的“行索引”数组,并通过它而不是m。

...
float *m_rows[4] = { m[0], m[1], m[2], m[3] };
foo(m_rows);

There no way to pass m to foo directly. It is impossible. The parameter type float ** is hopelessly incompatible with the argument type float [4][4].

没有办法直接把m传递给foo。这是不可能的。参数类型float **与参数类型float[4][4]完全不兼容。

Also, since C99 the above can be expressed in a more compact fashion as

而且,由于C99可以用更紧凑的方式表示

foo((float *[]) { m[0], m[1], m[2], m[3] });

P.S. If you look carefully, you'll that this is basically the same thing as what Carl Norum suggested in his answer. Except that Carl is malloc-ing the array memory, which is not absolutely necessary.

注:如果你仔细看的话,你会发现这基本上和卡尔·诺鲁姆在他的回答中提到的一样。除了卡尔是mallocing的数组内存,这不是绝对必要的。

#2


8  

If you can't change foo(), you will need to change m. Declare it as float **m, and allocate the memory appropriately. Then call foo(). Something like:

如果不能更改foo(),则需要更改m。将其声明为float **m,并适当分配内存。然后调用foo()。喜欢的东西:

float **m = malloc(4 * sizeof(float *));
int i, j;
for (i = 0; i < 4; i++)
{
    m[i] = malloc(4 * sizeof(float));
    for (j = 0; j < 4; j++)
    {
        m[i][j] = i + j;
    }
}

Don't forget to free() afterwards!

不要忘记之后释放()!

#3


3  

You can't. m is not compatible with the argument to foo. You'd have to use a temporary array of pointers.

你不能。m与foo的参数不相容。你必须使用一个临时指针数组。

int main()
{
    float m[4][4];
    int i,j;

    float *p[4];

    p[0] = m[0];
    p[1] = m[1];
    p[2] = m[2];
    p[3] = m[3];

    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
            m[i][j] = i+j;


    foo(p);

#4


2  

If you have a compiler that supports C99, the current C standard, then you can do this:

如果您有一个支持C99(当前的C标准)的编译器,那么您可以这样做:

foo((float *[]){ m[0], m[1], m[2], m[3] });

(Note that this is exactly the same as AndreyT's answer, except that it forgoes having to name the temporary array)

(注意,这与AndreyT的答案完全相同,只是它放弃了给临时数组命名)

#5


2  

  • you dont need to do any changes in the main,but you function will work properly if you change the formal prototype of your function to (*pm)[4] or pm[][4] because **pm means pointer to pointer of integer while (*pm)[4] or pm[][4] means pointer to poiner of 4 integers .

    您不需要在main中做任何更改,但是如果您将函数的正式原型更改为(*pm)[4]或pm[4],那么您的函数将会正常工作,因为**pm意味着指向整数指针的指针,而(*pm)[4]或pm[4]意味着指向4个整数的指针。

    m here is also a pointer to pointer of 4 integers and not pointer to pointer of integers and hence not compatible.

    这里的m也是指向4个整数的指针,而不是指向整数的指针,因此不兼容。

    #include<stdio.h>
    void foo(float (*pm)[4])
    {
        int i,j;
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                printf("%f\n", pm[i][j]);
    
    }
    
    int main ()
    {
        float m[4][4];
        int i,j;
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                    m[i][j] = i+j;
    
        foo(m);
     }
    

#6


0  

Does foo(m) not work?

foo(m)不工作吗?

#7


0  

void foo(float **pm) is the same as void foo(float *pm[]) which is not a two-dimensional array of floats. It is an array of float*. Now, those float* may themselves point to float arrays, but that's a separate matter.

void foo(float **pm)与void foo(float *pm[])是相同的,它不是一个二维的浮点数组。它是一个浮点数组*。现在,这些浮点*本身可能指向浮点数组,但这是另一回事。

#8


0  

typedef float Float4[4];

void foo(Float4 *pm)
{
  int i,j;
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      printf("%f\n", pm[i][j]);
}

main()
{
  Float4 m[4];

  int i,j;
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      m[i][j] = i+j;

  foo(m);
  return 0;
}

#9


0  

Using C99 which supports run-time sized arrays, the following is a possible way to pass a 2-dim array:

使用支持运行时大小的数组的C99,可以通过以下方法传递一个2-dim数组:

void foo(void *pm, int row, int col)
{
    float (*m)[col] = pm;

    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            printf("%4.1f%s", m[i][j], (j == col-1)?"\n":" ");

}

int main()
{
    float m[4][4];

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            m[i][j] = i+j;

    foo(m, 4, 4);

    return 0;
}

#1


15  

If you insist on the above declaration of foo, i.e.

如你坚持上述“foo”的声明,即

void foo(float **pm)

and on using a built-in 2D array, i.e.

使用内置的2D数组。

float m[4][4];

then the only way to make your foo work with m is to create an extra "row index" array and pass it instead of m

然后,让你的foo与m一起工作的唯一方法是创建一个额外的“行索引”数组,并通过它而不是m。

...
float *m_rows[4] = { m[0], m[1], m[2], m[3] };
foo(m_rows);

There no way to pass m to foo directly. It is impossible. The parameter type float ** is hopelessly incompatible with the argument type float [4][4].

没有办法直接把m传递给foo。这是不可能的。参数类型float **与参数类型float[4][4]完全不兼容。

Also, since C99 the above can be expressed in a more compact fashion as

而且,由于C99可以用更紧凑的方式表示

foo((float *[]) { m[0], m[1], m[2], m[3] });

P.S. If you look carefully, you'll that this is basically the same thing as what Carl Norum suggested in his answer. Except that Carl is malloc-ing the array memory, which is not absolutely necessary.

注:如果你仔细看的话,你会发现这基本上和卡尔·诺鲁姆在他的回答中提到的一样。除了卡尔是mallocing的数组内存,这不是绝对必要的。

#2


8  

If you can't change foo(), you will need to change m. Declare it as float **m, and allocate the memory appropriately. Then call foo(). Something like:

如果不能更改foo(),则需要更改m。将其声明为float **m,并适当分配内存。然后调用foo()。喜欢的东西:

float **m = malloc(4 * sizeof(float *));
int i, j;
for (i = 0; i < 4; i++)
{
    m[i] = malloc(4 * sizeof(float));
    for (j = 0; j < 4; j++)
    {
        m[i][j] = i + j;
    }
}

Don't forget to free() afterwards!

不要忘记之后释放()!

#3


3  

You can't. m is not compatible with the argument to foo. You'd have to use a temporary array of pointers.

你不能。m与foo的参数不相容。你必须使用一个临时指针数组。

int main()
{
    float m[4][4];
    int i,j;

    float *p[4];

    p[0] = m[0];
    p[1] = m[1];
    p[2] = m[2];
    p[3] = m[3];

    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
            m[i][j] = i+j;


    foo(p);

#4


2  

If you have a compiler that supports C99, the current C standard, then you can do this:

如果您有一个支持C99(当前的C标准)的编译器,那么您可以这样做:

foo((float *[]){ m[0], m[1], m[2], m[3] });

(Note that this is exactly the same as AndreyT's answer, except that it forgoes having to name the temporary array)

(注意,这与AndreyT的答案完全相同,只是它放弃了给临时数组命名)

#5


2  

  • you dont need to do any changes in the main,but you function will work properly if you change the formal prototype of your function to (*pm)[4] or pm[][4] because **pm means pointer to pointer of integer while (*pm)[4] or pm[][4] means pointer to poiner of 4 integers .

    您不需要在main中做任何更改,但是如果您将函数的正式原型更改为(*pm)[4]或pm[4],那么您的函数将会正常工作,因为**pm意味着指向整数指针的指针,而(*pm)[4]或pm[4]意味着指向4个整数的指针。

    m here is also a pointer to pointer of 4 integers and not pointer to pointer of integers and hence not compatible.

    这里的m也是指向4个整数的指针,而不是指向整数的指针,因此不兼容。

    #include<stdio.h>
    void foo(float (*pm)[4])
    {
        int i,j;
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                printf("%f\n", pm[i][j]);
    
    }
    
    int main ()
    {
        float m[4][4];
        int i,j;
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                    m[i][j] = i+j;
    
        foo(m);
     }
    

#6


0  

Does foo(m) not work?

foo(m)不工作吗?

#7


0  

void foo(float **pm) is the same as void foo(float *pm[]) which is not a two-dimensional array of floats. It is an array of float*. Now, those float* may themselves point to float arrays, but that's a separate matter.

void foo(float **pm)与void foo(float *pm[])是相同的,它不是一个二维的浮点数组。它是一个浮点数组*。现在,这些浮点*本身可能指向浮点数组,但这是另一回事。

#8


0  

typedef float Float4[4];

void foo(Float4 *pm)
{
  int i,j;
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      printf("%f\n", pm[i][j]);
}

main()
{
  Float4 m[4];

  int i,j;
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      m[i][j] = i+j;

  foo(m);
  return 0;
}

#9


0  

Using C99 which supports run-time sized arrays, the following is a possible way to pass a 2-dim array:

使用支持运行时大小的数组的C99,可以通过以下方法传递一个2-dim数组:

void foo(void *pm, int row, int col)
{
    float (*m)[col] = pm;

    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            printf("%4.1f%s", m[i][j], (j == col-1)?"\n":" ");

}

int main()
{
    float m[4][4];

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            m[i][j] = i+j;

    foo(m, 4, 4);

    return 0;
}