Initialiser un string dans une fonction ?
Bonjour,
Je voila j'aimerais faire un programme simple qui choisi aléatoirement un mot et le stock dans une variable crée préalablement et je bloque vraiment, je début et j'ai encore du mal avec les pointeurs ducoup j'ai vraiment tester beaucoup de chose. Voici mon code :
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
| #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
//fonction cherchant un nombre aléatoirement.
int ALEA()
{
srand(time(NULL));
int nombre = rand()%9;
return nombre;
}
//fonction retournant un mot choisi au hasard dans le "dico".
void motChoisi(char* motChoisi,char **dico)
{
int indiceMots;
indiceMots= ALEA();
int taille = 0;
while (dico[indiceMots][taille] !='\0')
{
taille++;
}
motChoisi = malloc(sizeof(char)*(taille+1));
int i = 0;
while(dico[indiceMots][i] != '\0')
{
motChoisi[i]=dico[indiceMots][i];
i++;
}
motChoisi[i]='\0';
}
int main(int argc, char const *argv[])
{
char *dico[] = {"UN", "DEUX", "TROIS", "QUATRE", "CINQ", "SIX", "SEPT", "HUIT", "NEUF", "DIX"};
char *motChoisi;
motChoisi(motChoisi, dico);// l'erreur pointe vers cette ligne
printf("%s\n",motChoisi);
return 0;
} |
Gcc me renvoie une erreur : error: called object type 'char *' is not a function or function pointer
Est-ce que quelqu'un pourrais m'expliquer mon erreur ?
Merci de votre aide.