Salut à tous,
Je fais du c++ depuis quelques temps mais je bloque sur un problème qui me parait pourtant simple. Voici le code, puis j'explique où je ne comprends pas :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <iostream>
#include <iomanip>
 
using namespace std;
 
//***** Première fonction 'création'
int ** création ( int x, int y )
{
	int** tableau = new int*[y];
	for ( int i =0; i < y; i++ )
		tableau[i] = new int[x];
 
	for ( int i = 0; i < y; i++ )
		for ( int j =0; j < x; j++ )
			tableau[i][j] = i + 10*j;
 
	return tableau;
}
 
//***** Deuxième fonction 'création'
void création ( int ** tableau, int x, int y )
{
	tableau = new int*[y];
	for ( int i =0; i < y; i++ )
		tableau[i] = new int[x];
 
	for ( int i = 0; i < y; i++ )
		for ( int j =0; j < x; j++ )
			tableau[i][j] = i + 10*j;
}
 
void efface ( int ** tableau, int y )
{
	if ( tableau )
	{
		for ( int i = 0; i < y; i++ )
			delete [] tableau[i];
		delete [] tableau;
		tableau = 0;
	}
}
 
void affiche ( int ** tableau, int x, int y )
{
	if ( tableau )
	{
		cout << "Tableau :" << endl;
 
		for ( int i = 0; i < y; i++ )
		{
			for ( int j =0; j < x; j++ )
				cout << setw (3) << tableau[i][j];
			cout << endl;
		}
	}
}
 
//****************
//***** main *****
//****************
void main ()
{
	int ** tab01 = 0;
	int ** tab02 = 0;
 
	tab01 = création ( 3, 5 );
	affiche ( tab01, 3, 5 );
 
	création ( tab02, 4, 6 );//***** Pb ici *****
	affiche ( tab02, 4, 6 );
 
	efface ( tab01, 5 );
	efface ( tab02, 6 );
 
	system ("pause");
}
Mais pourquoi donc, après l'éxécution, dans 'main', de la ligne
création ( tab02, 4, 6 );//***** Pb ici *****
le pointeur tab02 reste-t-il désespérément vide alors que dans la fonction 'void création ( int ** tableau, int x, int y )', on initialise correctement la pointeur. Puisque l'opérateur new est utilisé, l'objet créé n'est pas éffacé à la fin de la fonction!! ( Où est-il donc passé? )

Ici le problème est simplifié, mais dans un programme que j'écris, je crée une fonction qui dois initialiser 3 pointeurs comment faire?