1
I’m having trouble trying to initialize a vector of strings my code is like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *str[3];
printf("DIGITE QUALQUER STRING: ");
scanf("%s", str[0]);
printf("%s\n", str[0]);
printf("DIGITE OUTRA STRING: ");
scanf("%s", str[1]);
printf("%s\n", str[1]);
printf("DIGITE MAIS OUTRA: ");
scanf("%s", str[2]);
printf("%s\n", str[2]);
/*
char *str[2];
str[0] = "abc";
str[1] = "cba";
printf("%s\n", str[0]);
printf("%s", str[1]);
*/
return 0;
}
When I compile this code the compiler returns me no warning and no error, when I run it and enter the first value it stops and exits the program so if I decrease my vector to only 2 and enter the first value it returns me memory junk and the second returns me to string past tense.
If instead of vector I do with just char *str
one string it returns me the past value normally I’m trying to catch the dynamic allocation mace I’m almost sure I’m doing something wrong.
You want to allocate the string in the stack or in the heap?
– Maniero