0
int main()
{
    char** subject_array;
    char** courses_array;    

    initialize(subject_array, courses_array);
}


void initialize(char*** subject_array, char*** courses_array)
{
    int i;

    *subject_array = (char**) malloc(100 * sizeof(char*)); // 100 char pointers
    *courses_array = (char**) malloc(100 * sizeof(char*));

    for(i = 0; i < 100; i++) //malloc for subject_array
    {
        (*subject_array)[i] = (char*) malloc(4 * sizeof(char)); // 4 chars for each
        (*courses_array)[i] = (char*) malloc(6 * sizeof(char)); // char pointer
    }

} //void initialize

My question is: Why is there a discrepancy between the declared double pointers in main and the triple pointers in the initialize function? I think the triple pointer allows me to modify where the 2d array points.

My assignment gives these requirements:

Since you will be changing to where the pointers point, this will involve triple pointers for subjects and courses!

The above code takes care of bolded requirement but I have absolutely no idea what this even means. Can someone break it down to me? Does that mean I can modify contents of the 2d arrays of subject_array and courses_array? Such as swapping? But I can do that already with just double pointers making a 2d array! WHY triple pointers?

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
JackW
  • 49
  • 6
  • [You're not the only one.](http://c2.com/cgi/wiki?ThreeStarProgrammer) – The Paramagnetic Croissant Aug 29 '15 at 05:09
  • [Don't cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Aug 29 '15 at 06:04
  • I've been programming C since its' inception. I have never had to write more than two pointer depth in all that time. However, this line: `initialize(subject_array, courses_array);` fails to add the third pointer. it should be: `initialize(&subject_array, &courses_array);` – user3629249 Aug 30 '15 at 14:36
  • the expression: `sizeof(char)` is defined as 1 and makes absolutely not difference to the value passed to `malloc()` and it does clutter the code. Suggest removing this expression from all calls to `malloc()`. At each call to `malloc`, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Aug 30 '15 at 14:42
  • at the end of the `main()` function all that malloc'd memory needs to be passed to `free()` to avoid a memory leak. – user3629249 Aug 30 '15 at 14:44
  • there are 'magic' numbers 4, 6, 100 in the posted code. 'magic' numbers are always a bad idea (except for 0 and 1) suggest adding #define statements with meaningful names for each of those magic numbers. and use those meaningful names throughout the code. When #define'ing numeric values, surround the value with parens '(' ')' to avoid any 'text replacement' problems. – user3629249 Aug 30 '15 at 14:48
  • "Why triple pointers?" probably because the instructor wanted to be sure you understood pointers in C. – user3629249 Aug 30 '15 at 15:07

1 Answers1

2

First of all, the code is incorret, because initialize asks for char***'s and main gives it char**'s. To add another "pointer level", you take the address of whatever you want to give it to. So, the last line of main shall read initialize(&subject_array, &courses_array) (or shall it be return 0?)

After all, if you don't need to modify the variable subject_array and courses_array themselves, not what they point to, you may remove those pointers. And you actually do modify those variables themselves by means of *var = malloc(...). And, as I already noted, the only problem is that the arguments to initialize are char***, char***, not char**, char** as main is doing.

3442
  • 8,248
  • 2
  • 19
  • 41