I am trying to split a string delimited by '\n' into an array of strings. The string represents an NxN rectangle so each row on the matrix will contain the same number of characters. This is what I have tried:
char **string_to_tab(char *str, int width, int height)
{
int i; //counter to scan str
int x; //counter for tab column no.
int y; //counter for tab row no.
char **tab;
i = 0; //I initialise variables
x = 0; //separately because I
y = 0; //like to :P
tab = (char**)malloc(sizeof(char) * height * width);
while (y < height)
{
while (x < width)
{
if (str[i] != '\n' || !(str[i]))
{
tab[y][x] = str[i]; //assign char to char* array
x++;
}
i++;
}
x = 0;
y++;
}
return (tab);
}
This gets me a segmentation fault, calling it would look something like this:
char *str = "+--+\n| |\n| |\n+--+";
char **matrix = string_to_tab(str, 4, 4);