3
After compiling the program, you receive 2 warnings of the same type:
"Warning: 'phrase' is used uninitialized in this Function"
and
"Warning: 'word' is used uninitialized in this Function"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *alocar_palavra(char *palavra);
char *alocar_frase(char *frase);
int main(){
char *frase, *palavra;
frase = alocar_frase(frase);
palavra = alocar_palavra(palavra);
printf("%s", frase);
printf("%s", palavra);
return 0;
}
char *alocar_palavra(char *palavra){
palavra = (char*)malloc(20 * sizeof(char));
scanf(" %s", palavra);
return palavra;
}
char *alocar_frase(char *frase){
char letra;
int k = 0;
frase = NULL;
do{
letra = getchar();
frase = (char*)realloc(frase, (k+1)*sizeof(char));
frase[k] = letra;
k++;
}while((letra != '$') && (letra != '\n'));
return frase;
}
realloc
can only be called aftermalloc
,calloc
, orrealloc
as indicated in the documentation, otherwise you can get undetermined results– Isac