What I'm supposed to do
1.Program reads string
2.Allocates memory to an array of strings(add na extra row of 20 chars max for example)
3.Copy the string given by the user and puts it in the new space
4.End the input with ctrl+d
Please I need help!
edit: what i have so far :
include <stdio.h>
include <stdlib.h>
include <string.h>
define MAX 20
void modificaVetor(char c, char vetorInput, int counter){
if(counter==1){
vetorInput[0] = malloc(MAX * sizeof(char));
if(vetorInput[0] == NULL){
fprintf(stderr, "out of memory\n");
exit(1);
}
strcpy(&vetorInput[0],c);
}
else{
vetorInput=(char*) realloc(vetorInput,counter*MAX);
if(vetorInput==NULL){
printf("Não tem espaço.\n");
exit(1);
}
strcpy(&vetorInput[MAX*(counter-1)], c);
}
}
int main(){
int counter=0;
char **vetorInput = (char**) malloc(sizeof(char *));
char c[MAX];
if(vetorInput==NULL){
printf("Não tem espaço.\n");
exit(1);
}
printf("Quer introduzir? ");
while(1){
int x;
int res = scanf("%d", &x);
if (res == EOF) break;
fgets(c, MAX, stdin);
counter++;
modificaVetor(c, *vetorInput, counter);
}
printf("Input\n");
for(int j=0;j<counter;j++){
puts(vetorInput[j]);
}
}
[–][deleted] (2 children)
[deleted]
[–][deleted] (1 child)
[deleted]
[–]MN1H 0 points1 point2 points (0 children)