I am trying to split a string inputted into a 3d array, is soon as a new entry has been found when strtoking it then reallocing will come into play to compensate for the entry.
If it detects a '>' in the string inputted, the row will increase by 1 and realloc the array.
If it detects a another entry delimited by a ' ' space, it will realloc the element count enclosed in each row by 1.
I want to use dynamic memory allocation so that unlim entries can be added to the memory.
E.g: input= 12 22 33 44 53 > 111 233 31 4 25 33 11 > 24 32 24 3 21 32
First block
First entry: [0][0]
Second entry: [0][1]
.....
Second block
First entry: [1][0]
Second entry: [1][1]
.....
etc.....
Code:
/* Headers here */
#define ROWS 1
main(int argc, char **argv)
{
char ***commands;
char *temp;
char *split =NULL;
int commandinc =0,element=0;
/* File read into the temp variable here */
/* Malloc a 1 row array here */
commands = malloc(ROWS*sizeof(char *));
split = strtok(temp, " ");
while(split != NULL )
{
if(strcmp(split,">") != NULL)
{
element =0;
/* Add new row by one [e.g: commands[1] */
commandinc++;
}
/* Realloc the element count and add a element to the row*/
commands[commandinc][element] = line;
split = strtok(NULL, "\n");
element++;
}
return 0;
}
Any help will be greatly appreciated, thanks
it01y2