Structure, pointeur et fonction...
Bonsoir,
J'ai un problème avec mon programme.
Alors j'ai un .h avec
Code:
1 2 3 4 5 6 7 8 9
| struct CLIENT {
int * t_num;
float * t_achats;
int nb_max_clients;
};
typedef struct CLIENT TAB_CLIENTS;
void init (int, TAB_CLIENTS *); |
C'est le fait de passer ma variable structure en pointeur à la fonction init qui pose problème!
Ma fonction init :
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
| void init (int max_clients, TAB_CLIENTS * cli) {
// declarations
int i, num[max_clients];
float achats[max_clients];
cli->nb_max_clients=max_clients;
// initialisation des numeros
for(i=0;i<max_clients;i++)
num[i]=0;
cli->t_num = num;
// initialisation des achats
for(i=0;i<max_clients;i++)
achats[i]=0.0;
cli->t_achats = achats;
// test d'affichage
for(i=0;i<cli->nb_max_clients;i++)
printf("n° %d : %f\n",cli->t_num[i],cli->t_achats[i]);
printf("\n\n");
} |
Avec le test d'affichage, tout est ok, ma variable structure est bien initialisée!
Le problème est dans mon main
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int main () {
// declarations
int i;
TAB_CLIENTS les_clients;
// appel a la fonction
init(10,&les_clients);
// test d'affichage qui foire!
for(i=0;i<les_clients.nb_max_clients;i++)
printf("n° %d : %f\n",les_clients.t_num[i],les_clients.t_achats[i]);
return 0;
} |
La variable les_clients que je recupère devrait etre initialisée, mais en fait elle l'est à moitié :(
Code:
1 2 3 4 5 6 7 8 9 10
| n° 0 : 0.000000
n° 32 : 0.000000
n° 1107378272 : 0.000000
n° 1108287296 : 0.000000
n° 0 : 0.000000
n° 0 : 0.000000
n° 0 : 0.000000
n° 0 : 0.000000
n° 0 : 0.000000
n° 0 : 0.000000 |
J'ai réduit le nombre max, mais avec 30 par ex, c'est pire ! Il y a meme des nombres negatifs qui apparaissent!
Je ne vois pas où est l'erreur ?! Si vous pouvez m'éclairer.
Merci