I have a goal of statically generating some large arrays of geographic coordinates with some minimal metadata (source is GeoJSON but it's irrelevant). I'm trying to figure out how to format my data, and want something resembling an array of structs, where each struct contains metadata, and a pointer to coordinates. Something like the following seems to work:
#include <stdint.h>
#include <stdio.h>
float coords1[3][2] =
{
{123.4f, 321.3f},
{65.21, 322.3f},
{01.2f, 21.0f},
};
float coords2[5][2] =
{
{132.2f, 3.3f},
{6.121, 32.3f},
{01.232f, 211.0f},
{0.2f, 0.3f},
{0.121, 0.3f},
};
/* Hundreds of coordinate arrays */
/* Section definition */
typedef struct
{
uint32_t id;
uint32_t coords_num;
float * coords_p;
} section;
/* Array of sections to be rendered */
section secs[2] = { {1, 3, &(coords1[0][0])},
{1, 5, &(coords2[0][0])}};
int main(void)
{
printf("sec2: %f, %f\n", secs[1].coords_p[0], secs[1].coords_p[1]);
}
What I do not like is the fact that the pointer in the struct loses the 2D array properties, and becomes a dumb pointer to a float array. I would like it to still be a 2D array instead (albeit incomplete, something of type float[][2]), since that would at least inform the compiler of the pointer math, and I can do something like coords_p[4][1] to access a coordinate, instead of doing pointer arithmetic. Ideally there would be static bounds checking also. I can't seem to figure out a way to do that in C however.