passage de pointeurs avec realloc
Bonjour,
J'ai un problème débile dû, je pense, à une mauvaise manipulations des pointeurs mais je n'arrive pas à trouver de solution.
J'espère que le problème va vous sauter aux yeux!
Je souhaite utiliser depuis le main ce que GetCommandesString m'a remplit dans commands. Dans le printf de GetCommandesString j'ai bien remplit mon tableau mais lorsque je l'imprime dans le main il est inexact.
Je me demande si ce n'est pas parceque le realloc met le pointeur ailleurs.
Si vous avez des idées... je suis sûre que c'est une question bête...
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 38 39 40 41 42
|
int main(void)
{
//creation tableau
struct command *commands = malloc(1*sizeof(struct command));
short nombreCommandes=GetCommandesString("fichier.par",commands);
if(nombreCommandes==-1)
{
exit(EXIT_FAILURE);
}
printf("commands[%d].arg : %s \n", nombreCommandes,commands[nombreCommandes].arg);
}
short GetCommandesString( char * file, struct command *commands)
{
short nombreCommands=-1;
FILE * fp = fopen( file , "r" );
char * aux;
memset(buffer, 0, sizeof(buffer));
while(fgets( buffer , sizeof( buffer ) , fp ))
{
nombreCommands++;
commands = (struct command *)realloc(commands,(nombreCommands+1)*sizeof(struct command));
if(!strncmp( buffer, "arg",3)&&(nombreCommands!=-1))
{
if(strlen(buffer)) buffer[strlen( buffer) - 1 ] = 0;
aux = strchr( buffer , '=' );
aux=trim(aux+1);
strcpy(commands[nombreCommands].arg, aux);
printf("NV ARG de commande: %s \n", commands[nombreCommands].arg);
}
}
fclose( fp );
return nombreCommands;
} |
Merci d'avance