i wish to store n number of words to an array. I used pointer in the following code to do that .But the problem is that once all words are read the words are not able to print.
please provide any solution.
#include <stdio.h>
#include <stdlib.h>
int main() {
char buff[10];
int i,T;
printf("Enter the no.of words:");
scanf("%d",&T);
char **word= malloc(10*T);
printf("Enter the words:\n");
for(i=0;i<T ;i++){
scanf("%s",buff);
word[i]= malloc(sizeof(char)*10);
word[i]=buff;
printf("u entered %s\n",word[i]);
if(i>0)
printf("u entered %s\n",word[i-1]);
}
printf("Entered words are:\n");
for(i=0;i<T ;i++)
printf("%s\n",word[i]);
}
buffpointer to each array element. That obviously means they all contain the same value and also causes a memory leak as themallocbuffer is lost. Usestrcpyorstrdup.