1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <QCoreApplication>
#include <iostream>
using namespace std; // std::array
void function1(int (**gridPt)[7])
{
*gridPt = new int[5][7];
for (int i = 0 ; i < 5 ; ++i)
for (int j = 0 ; j < 7 ; ++j)
(*gridPt)[i][j] = 0;
(*gridPt)[4][6] = 5;
}
// Cette fonction ne marche pas.
void function2(int (**gridPt)[][colsNb], const int rowsNb, const int colsNb)
{
*gridPt = new int[rowsNb][colsNb];
for (int i = 0 ; i < rowsNb ; ++i)
for (int j = 0 ; j < colsNb ; ++j)
(*gridPt)[i][j] = 0;
(*gridPt)[4][6] = 6;
}
// Méthode 1 : typedef
typedef int Array7Int[7];
Array7Int* myfunction(int rowsNb) // Type souhaité à retourner : int[5][7], remplacé par int (*)[7]
{
Array7Int* grid = (Array7Int*)malloc(rowsNb * sizeof(Array7Int)); // Alloue rowsNb éléments de type Array7Int
return grid;
}
// Méthode 2 : array<array<int,5>,7>
#include <array> // array<array<int,...>,...>
int main(int argc, char *argv[])
{
QCoreApplication qca(argc, argv);
int i,j;
// Variables à déclarer constantes
const int rowsNb = 5;
const int colsNb = 7;
// Syntaxe
int *M1 [7]; // tableau de 7 pointeurs de int
int (*M2)[7]; // pointeur de tableaux de 7 int
int n = 5; // acquisition de la valeur de n
M2 = new int[n][7]; // allocation d'un tableau de n tableaux de 7 int
// Avoid warning compiler
for (i = 0 ; i < 7 ; ++i)
M1[i] = NULL;
// Méthode 1 : typedef
Array7Int* grid1 = myfunction(5); // myfunction(5) retourne un int(*)[7] qui a 5 éléments
grid1[4][6] = 1;
// Méthode 2 : std::array<std::array<int,5>,7>
using Grid57 = array<array<int,7>,5>; // Sans doute équivalent à typedef array<array<int,5>,7> Grid75;
Grid57 grid2; // pas d'allocation dynamique, peut être copié, et transmis et reçu de fonction
grid2[4][6] = 2; // mais moment de la destruction inconnue
// Méthode 3 : Sans using
array<array<int,7>,5> grid3;
grid3[4][6] = 3;
// Méthode 4 : Sans appel de fonction
int (*grid4)[colsNb] = new int[rowsNb][colsNb];
for (int i = 0 ; i < rowsNb ; ++i)
for (int j = 0 ; j < colsNb ; ++j)
grid4[i][j] = 0;
grid4[4][6] = 4;
// Méthode 5 : Avec appel de fonction - rowsNb et colsNb fixes
int (*grid5)[colsNb];
function1(&grid5);
// Méthode 6 : Avec appel de fonction - rowsNb et colsNb variables
int (*grid6)[colsNb];
function2(&grid6,rowsNb,colsNb);
// Méthode 7 : Avec using et appel de fonction - rowsNb et colsNb variables
using GridRC = array<array<int,rowsNb>,colsNb>;
int (*grid7)[colsNb];
function2(&grid7,rowsNb,colsNb);
// Affichage des grid
for ( i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid1[i][j]);
for (printf("\n") , i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid2[i][j]);
for (printf("\n") , i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid3[i][j]);
for (printf("\n") , i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid4[i][j]);
for (printf("\n") , i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid5[i][j]);
for (printf("\n") , i = 0 ; i < rowsNb ; ++i)
for (j = 0 ; j < colsNb ; ++j , printf("\n"))
printf("%d ",grid6[i][j]);
// Ne pas oublier
delete [] grid1;
// delete [] grid2; // Sans doute effectué lorsque la variable n'est pas plus utilisée dans la fonction appelante.
// delete [] grid3; // Sans doute effectué lorsque la variable n'est pas plus utilisée dans la fonction appelante.
delete [] grid4;
delete [] grid5;
delete [] grid6;
return qca.exec();
} |
Partager