I am trying to make a function which writes strings and integers to a file. A part of my approach involves creating a 2-D dynamic array to hold a series of strings. There are no compile time errors. However, when I try to run the program (the function plus a main to test it inside) I keep getting a segmentation fault error. I have traced it so far to a particular section of code that I have highlighted below. From what I can tell, it seems that my attempts to open the array are what is causing the seg fault (I have used a debug string with a %p character in it to make sure that the array is actually in the memory). I can't for the life of me tell why though. For convenience, I've only included the code that pertains to the seg fault as well as the declarations/initializations. If you want me to provide the whole main, please let me know. To be clear, I have tried Google for a solution, but everything I've found so far either I don't understand, or doesn't pertain to my situation.
int data_cat_num=0,current_cat=0,col_index=0;
char **cat_names;
printf("How many data categories will there be?:");
scanf("%d",&data_cat_num);
cat_names=malloc(data_cat_num*50);/*Currently defaulting to 50 bytes per name*/
while(current_cat<data_cat_num){
col_index=0;
printf("What is the name of category %d?",current_cat+1);
while(cat_names[current_cat][col_index]!='\n'){/*SEG FAULT TRACED TO THIS*/
cat_names[current_cat][col_index++]=getchar();/*LOOP!!!!!*/
}
cat_names[current_cat][col_index]='\n';
current_cat++;
}