pointeur pour un tableau 2d
Bonjour,
Je voudrais realiser un pointeur sur un tableau 2d pour le passer comme argument à une fonction.
Voici un exemple de ce que je veut faire.
Code:
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
| #include <stdio.h>
#include <stdlib.h>
/*prototype de la fonction*/
void initialisation(int **);
int main(int argc, char *argv[])
{
int tab[10][10];
int **point; /*mon pointeur sur le tableau 2d*/
point=&tab[0][0];
initilisation(**point);
system("PAUSE");
return 0;
}
/*ma fonction qui recoit le pointeur en argument*/
void initialisation(int **p_point)
{
int i,j,b;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
p_point[i][j]=0;
b=p_point[i][j];
printf("%d",b);
}
printf("\n");
}
} |
Re: pointeur pour un tableau 2d
Citation:
Envoyé par olive14
Je voudrais realiser un pointeur sur un tableau 2d pour le passer comme argument à une fonction.
Code:
1 2 3 4 5
|
int tab[10][10];
int **point; /*mon pointeur sur le tableau 2d*/
point=&tab[0][0]; |
Non, mauvais type. Un pointeur de pointeur n'est en aucun cas compatible avec un tableau linéaire à deux dimensions. Si tu as lu ça sur un site ou dans unlivre, on veux des réferences. Des sanctions s'imposent!
Ceci fonctionne, mais c'est un peu tordu...
Code:
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
|
#include <stdio.h>
/* ma fonction qui recoit l'adresse en argument */
void initialisation (int (*p_point)[10][10])
{
int i, j, b;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
(*p_point)[i][j] = i + j;
b = (*p_point)[i][j];
printf ("%2d ", b);
}
printf ("\n");
}
}
int main (int argc, char *argv[])
{
int tab[10][10];
/* mon pointeur sur le tableau 2d */
int (*point)[][10];
point = &tab;
initialisation (point);
return 0;
} |
Je préfère encore ça:
Code:
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
|
#include <stdio.h>
/* ma fonction qui recoit l'adresse en argument */
void initialisation (int tab[10][10])
{
int i, j, b;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
tab[i][j] = i + j;
b = tab[i][j];
printf ("%2d ", b);
}
printf ("\n");
}
}
int main (int argc, char *argv[])
{
int tab[10][10];
initialisation (tab);
return 0;
} |