#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int size = 3;
char word[30];
char **word_list = malloc(sizeof(word_list) * size);
printf("Enter %d Words\n",size);
for(int i =0; i<size; ++i){
scanf("%30s",word);
int word_size = strlen(word)+1;
word_list[i] = malloc(word_size);
strncpy(word_list[i],word,word_size);
}
printf("\nEntered words are\n");
for(int i=0; i<size; ++i){
printf("%s ",word_list[i]);
}
//resizing the capacity of the array
{
int resize;
printf("\nresize array size to: ");
scanf("%d",&resize);
if(size<resize){
for(int i=size-1; i>=resize; --i)
free(word_list[i]);
}
word_list = realloc(word_list, sizeof(word_list) * resize);
if(resize > size)
printf("Enter %d Words \n",resize-size);
for(int i =size; i<resize; ++i){
scanf("%30s",word);
int word_size = strlen(word)+1;
word_list[i] = malloc(word_size);
strncpy(word_list[i],word,word_size);
}
size = resize;
}
printf("Current words are\n");
for(int i=0; i<size; ++i){
printf("%s ",word_list[i]);
}
//Memory deallocation
for(int i=0; i<size; ++i){
free(word_list[i]);
}
free(word_list);
return 0;
}