I'm writing how to dynamically allocate memory for n strings with each string be however long you want. So I know to use double pointers, but can't understand how to allocate each string with the right amount of space. My code:
int i,n;
char **str;
printf("Enter n (strings of chracters): ");
scanf("%d",&n);
str = (char**)malloc(n*sizeof(char*));
printf("Enter strings one by one:\n");
for(i=0; i<n; i++) {
str[i] = (char*)malloc(//SOMETHING//*sizeof(char));
gets(str[i]);
}
I tried to be cheeky and put gets() first like this
for(i=0; i<n; i++) {
gets(str[i]);
str[i] = (char*)malloc(strlen(str[i])*sizeof(char));
but that obviously is wrong, I can't read string then allocate it.
So is there anyway to replace //SOMETHING// with the length of each string?
Thanks for reading my amateur post.