bonjour amis :) je pratique la programmation en C. Dans ce programme, j'ai une tâche à faire un tableau de chaîne. Je ne sais pas quel est le problème ici ... probablement quelque chose realloc, erreur que je reçois est _crtisvalidheappointerC double pointeur erreur realloc
#define _CRT_SECURE_NO_WARNINGS
#define MAX 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readString(char **s)
{
int i = 0;
char c;
printf("\nInput string: ");
while ((c = getchar()) != '\n')
{
i++;
*s = realloc(*s, i*sizeof(char*));
if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
(*s)[i - 1] = c;
}
*s = realloc(*s, (i + 1)*sizeof(char));
if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
(*s)[i] = '\0';
}
char **load_words()
{
int cnt=0,wordcnt=0,i=0;
char **words = NULL, *input = NULL;
readString(&input);
while (input[cnt] != '\0' && cnt < strlen(input))
{
words = realloc(words, ++wordcnt);//errors in second repeat of the loop
words[wordcnt] = malloc(MAX);
i = 0;
while (input[cnt] != ' ')
{
words[wordcnt][i++] = input[cnt++];
}
words[wordcnt][i] = '\0';
realloc(words[wordcnt], (i + 1)*sizeof(char));
}
realloc(words, wordcnt);
free(input);
return words;
}
void main()
{
int i;
char **words = NULL;
words = load_words();
scanf("%d", &i);
}
quelqu'un peut me aider et dites-moi qu'est-ce que je fais mal ici? cette fonction doit renvoyer un tableau de chaînes mais array doit être un double pointeur (matrice de chaînes)
void readString (char ** s) fonctionne correctement – hd130664d
'' s = realloc (* s, i * sizeof (char *)); 'et' * s = realloc (* s, (i + 1) * sizeof (char)); ':' char * 'vs' char', suspect. – MikeCAT