I am trying to write a c program, which will take an input from Linux pipe and puts each word up to the point when it encounters \n into arrays. Then it outputs the words. It works when array contain less than 3 \n but it gives me segmentation error if there is 3 \n`s in the input. Here is the code that I wrote, please help. I am new to programming,so please try to live the code as intact as possible.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int out=0;
int in=0;
char **arr2D;
int i;
int flag = 1;
arr2D=(char**)malloc(out*sizeof(char*));
for(i=0;i<out;i++)
arr2D[i]=(char*)malloc(in*sizeof(char*));
while (!feof(stdin))
{
in = in +1;
arr2D[out]=(char*)realloc(arr2D[out],(in)*sizeof(char*));
scanf("%c",&arr2D[out][i]);
i=i+1;
if(arr2D[out][i-1]=='\n')
{
out=out+1;
arr2D=(char**)realloc(arr2D,out*sizeof(char*));
i=0;
in=0;
}
}
int out2=0;
int in2=0;
do
{
do
{
printf("%c",arr2D[out2][in2]);
in2++;
}
while(in2<=in-1);
out2++;
in2=0;
}
while(out2<=out);
printf("\n");
return 0;
}