Is there something wrong that I am doing in the below code. It is throwing segmentation fault.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **arr, str1[10];
int i, a;
a = 20;
strcpy(str1,"hello");
for(i=0; i<a; i++)
{
printf("Before malloc...\n");
arr = malloc(10);
printf("after malloc...\n");
sprintf(arr, "%s", str1);
}
for(i=0; i<a; i++)
{
printf("%s\n", arr);
}
for(i=0; i<a; i++)
{
free(arr);
}
return(0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **arr, str1[10];
int i, a;
a = 20;
strcpy(str1,"hello");
for(i=0; i<a; i++)
{
printf("Before malloc...\n");
arr = malloc(10);
printf("after malloc...\n");
sprintf(arr, "%s", str1);
}
for(i=0; i<a; i++)
{
printf("%s\n", arr);
}
for(i=0; i<a; i++)
{
free(arr);
}
return(0);
}