Code:
void init(char***subjects_array,char***courses_array)
{
int i;
*subjects_array =(char**) malloc (100*sizeof(char*));
*courses_array =(char**) malloc(100*sizeof(char*));
for(i =0; i < INITIAL_COURSE_SIZE; i++)
{
(*subjects)[i]=(char*) malloc(4*sizeof(char));
(*courses)[i]=(char*) malloc(6*sizeof(char));
}
}// end of Triple pointer version
// Double Pointer Version
void init(char** subject_array,char** courses_array){
subject_array =(char**) malloc (100*sizeof(char*));
courses_array =(char**) malloc (100*sizeof(char*));
for(int i =0; i<100; i++){
subject_array[i]=(char*) malloc(4*sizeof(char));
courses_array[i]=(char*) malloc (6*sizeof(char));
}
}
My question is: Both versions initiate a 2d array. So what purpose does the triple pointer version serve? It seems that the extra level of indirection serves no "purpose".
In the triple pointer version, it would allow me to allocate double pointers...But in either version no double pointers were allocated. Could you point out where double pointers are assigned especially in the triple pointer version
Here was the explantation:
Code:
Since C passes parameters, including pointers, only by value, you need to add an extra level of indirection (i.e. an asterisk) for any parameter that you wish to modify inside a function.
This provides the clue to what's going on: normally, you need two asterisks for a 2D array, i.e. char **twoD. However, since init allocates a new array, you need an extra level of indirection, i.e.
void init(char***subjects_array,char***courses_array)