I am trying to write a function that assigns the elements of a row in a 2D array to a 1D array.
I have written my function as:
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];
}
}
I have been trying to call that function using:
storage_position_particle(position_reference_particle,
&box_pointer->positions_particles[][index_reference_particle]);
CLion flags the empty square brackets in &box_pointer->positions_particles[][index_reference_particle]
with "Expected expression." This project is being coded in C11.
Can anyone shed some light on how to do this elegantly and properly? Thanks ahead of time for any help you all can provide!!!
CodePudding user response:
The function can be declared and defined the following way (I am using your identifier names)
#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 ) );
}
To call the function there is no need to pass a whole two dimensional array or an array of pointers to arrays as the second argument. What you need is to pass only one "row" for example using the subscript operator.
For example if you have arrays
double a[NUMBER_SPATIAL_DIMENSIONS];
and
double b[NUMBER_SPATIAL_DIMENSIONS][NUMBER_SPATIAL_DIMENSIONS];
then the function can be called like
storage_position_particle( a, b[i], NUMBER_SPATIAL_DIMENSIONS );
where i
denotes a row number in the two dimensional array.
CodePudding user response:
Re &box_pointer->positions_particles[][index_reference_particle]
: You cannot do that. That looks like an attempt to extract a column of an array as a new array. C does not have any feature for that.
When used in an expression, the subscript operator, [ … ]
, must have an expression inside it. Hence the compiler is telling you it expected an expression. (In declarators, the brackets may be empty to indicate something is an array with undisclosed size.)
To copy a column, you will need to rewrite storage_position_particle
to accept a two-dimensional array and have it iterate through the individual assignments of the elements.
One way to do that is:
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];
}
}
which you would then call with storage_position_particle, destination, box_pointer->positions_particles, index_reference_particle)
.