il y a deux méthodes pour gérer ce genre de choses.
première méthode : utiliser un tableau 2D :
int main(...)
{
float terrain1[10][20]; // de taille connue à l'avance
float** terrain2; // de taille connue à l'exécution.
terrain2 = alloc_terrain(10,20);
affiche(terrain1,10,20);
affiche(terrain2,10,20);
detruit(terrain2,10);
}
float** alloc_terrain(int x, int y)
{
float** terrain = (float**)malloc(sizeof(float*)*x);
for (int i = 0; i < x; i++)
terrain[i] = (float*)malloc(sizeof(float)*y);
return terrain;
}
void affiche(float** terrain, int x, int y)
{
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
affiche_case(terrain[i][j]);
}
void detruit(float** terrain, int x)
{
for (int i = 0; i < x; i++)
free(terrain[i]);
free(terrain);
}
Voila tu a un exemple de création et de destruction pour ces tableaux plus des fonctions qui les reçoivent ou les retourne.
L'autre possibilité est d'utiliser des tableaux à une seule dimensions. C'est parfois préféré car ça peut-être plus performant (sans aucune garantie).
int main(...)
{
float terrain1[10*20]; // de taille connue à l'avance
float* terrain2; // de taille connue à l'exécution.
terrain2 = alloc_terrain(10,20);
affiche(terrain1,10,20);
affiche(terrain2,10,20);
detruit(terrain2);
}
float* alloc_terrain(int x, int y)
{
float* terrain = (float*)malloc(sizeof(float)*x*y);
return terrain;
}
void affiche(float* terrain, int x, int y)
{
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
affiche_case(terrain[i*x+j]);
}
void detruit(float* terrain)
{
free(terrain);
}