【问题标题】:How can I assign the elements of a row in a 2D array to a 1D array through a function in C (C11)?如何通过 C (C11) 中的函数将 2D 数组中的行的元素分配给 1D 数组?
【发布时间】:2021-12-23 09:46:01
【问题描述】:

我正在尝试编写一个函数,将二维数组中一行的元素分配给一维数组。

我的函数写成:

void storage_position_particle(double position_particle_stored[NUMBER_SPATIAL_DIMENSIONS],
                               double *positions_particles_list[NUMBER_SPATIAL_DIMENSIONS]) {
    int index_spatial_dimension;

    for (index_spatial_dimension = 0;
         index_spatial_dimension < NUMBER_SPATIAL_DIMENSIONS;
         index_spatial_dimension++) {
        position_particle_stored[index_spatial_dimension] =
                *positions_particles_list[index_spatial_dimension];
    }
}

我一直在尝试使用以下方法调用该函数:

storage_position_particle(position_reference_particle,
                          &box_pointer->positions_particles[][index_reference_particle]);

CLion 将 &amp;box_pointer-&gt;positions_particles[][index_reference_particle] 中的空方括号标记为“预期表达式”。这个项目是用 C11 编码的。

谁能解释一下如何优雅而正确地做到这一点?提前感谢大家可以提供的任何帮助!!!

【问题讨论】:

  • 这段代码中没有二维数组

标签: c multidimensional-array copy function-definition function-declaration


【解决方案1】:

函数可以通过以下方式声明和定义(我使用的是你的标识符名称)

#include <string.h>

//...

void storage_position_particle( double *position_particle_stored,
                                const double *positions_particles_list,
                                size_t NUMBER_SPATIAL_DIMENSIONS )
{
    memcpy( position_particle_stored, 
            positions_particles_list, 
            NUMBER_SPATIAL_DIMENSIONS * sizeof( double ) );
}

要调用该函数,无需将整个二维数组或指向数组的指针数组作为第二个参数传递。您只需要传递一个“行”,例如使用下标运算符。

例如,如果您有数组

double a[NUMBER_SPATIAL_DIMENSIONS];

double b[NUMBER_SPATIAL_DIMENSIONS][NUMBER_SPATIAL_DIMENSIONS];

然后函数可以像这样调用

storage_position_particle( a, b[i], NUMBER_SPATIAL_DIMENSIONS );

其中i 表示二维数组中的行号。

【讨论】:

    【解决方案2】:

    回复&amp;box_pointer-&gt;positions_particles[][index_reference_particle]:你不能那样做。这看起来像是试图将数组的列提取为新数组。 C 没有任何功能。

    在表达式中使用时,下标运算符[ … ] 中必须有一个表达式。因此编译器告诉你它需要一个表达式。 (在声明符中,括号可能是空的,表示某事物是一个未公开大小的数组。)

    要复制列,您需要重写 storage_position_particle 以接受二维数组并让它遍历元素的各个分配。

    一种方法是:

    void storage_position_particle(double position_particle_stored[NUMBER_SPATIAL_DIMENSIONS],
                                   double positions_particles_list[][NUMBER_SPATIAL_DIMENSIONS],
                                   int index_reference_particle) {
        int index_spatial_dimension;
    
        for (index_spatial_dimension = 0;
             index_spatial_dimension < NUMBER_SPATIAL_DIMENSIONS;
             index_spatial_dimension++) {
            position_particle_stored[index_spatial_dimension] =
                    positions_particles_list[index_spatial_dimension][index_reference_particle];
        }
    }
    

    然后您可以使用storage_position_particle, destination, box_pointer-&gt;positions_particles, index_reference_particle) 调用它。

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多