something like 20 hours have passed and i still dont manange to get what is the problem.
First of all, there is the main function with these parameters.
char ***matrix;
int line, maxCollumn, i, j;
int lineData[100];
Then i am calling the function
PutFirstAndLast(&matrix, &line, &maxCollumn, lineData);
This function is intended to dynamicly allocate my matrix and to return its number of lines and collumns. lineData is used to store how many items i have on each line because later in the function i will have to fill the "empty" spaces with "".
My troubling function has this header
int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)
In this function i will generate a new matrix called in a semi-retarded way "array", i will allocate memory for the new "array" in orded to put each word from a file "in.txt" in it.
int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)
{
FILE *inputFile;
char buffer[100], *p;
char ***array;
int i, j, collumn, maxCollumn = 0, line = -1;
inputFile = fopen("in.txt", "r");
while (fgets(buffer, 100, inputFile))
{
line++;
collumn = 0;
if (line == 0)
array = (char***)malloc(sizeof(char**));
else
array = (char***)realloc(array, sizeof(char**)* (line + 1) );
p = strtok(buffer, " \n");
while (p)
{
//taking each word and allocating a space for it
if (collumn == 0)
array[line] = (char**)malloc(sizeof(char*));
else
array[line] = (char**)realloc(array[line], sizeof(char*)* (collumn + 1));
array[line][collumn] = (char*)malloc(sizeof(char)* strlen(p) + 1);
strcpy(array[line][collumn], p);
collumn++;
p = strtok(NULL, " \n");
}
// Here i count what is the maximum numbers of items on a line
lineArray[line] = collumn;
if (collumn > maxCollumn)
maxCollumn = collumn;
}
// Here i add "" in the "empty spaces.
for (i = 0; i < line + 1; i++)
{
for (j = lineArray[i]; j < maxCollumn; j++)
{
array[i] = (char**)realloc(array[i], sizeof(char*)* maxCollumn);
array[i][j] = (char*)malloc(sizeof(char)* 10);
strcpy(array[i][j], "");
}
}
fclose(inputFile);
*matrixPointer = array;
*maxCollumn2 = maxCollumn;
*line2 = line;
return 0;
}
Problem is that when i come to main and i do this
for (i = 0; i < line + 1; i++)
{
for (j = 0; j < maxCollumn; j++)
{
printf("%s", matrix[i][j]);
matrix[i][j] = (char*)realloc(matrix, sizeof(char)* 11);
}
}
for item matrix[2][0] there is an error "Acces violation reading location".
bur if i do
for (i = 0; i < line + 1; i++)
{
for (j = 0; j < maxCollumn; j++)
{
printf("%s", matrix[i][j]);
// matrix[i][j] = (char*)realloc(matrix, sizeof(char)* 11);
}
}
there is no problem and all my words from the matrix are shown on the screen\
my model of text is:
etesta este astru
de mext pentru bun
examen multa programare
by adding "" in "empty" spaces i refer to : puttin the "" in the arraylike this:
etesta este astru
de mext pentru bun
examen multa programare