views:

219

answers:

2

Season's greetings! I have a function that prints out the contents of a char** that is being used as an array to store a number of strings. The is declared like this:

char** commandArray = (char**)malloc(historySize);

where historySize is a global int set to 8, for now. The array is populated with commands that the user enters, in sort of a circular queue. There are more than a couple places where I may want the contents of the buffer printed out, so I made a function. Ideally, the function takes in a reference to commandArray and loops through it, printing out what it contains. Now, I have to say that pointers and references are not my strong point, so I'm not really sure if I'm doing things correctly. The function looks like this:

    /* prints the contents of the history buffer */
void printHistory(char*** historyBuff)
{
    /* a counter for the loop */
    int loopIdx = 0;

    for (loopIdx = 0; loopIdx < historySize; loopIdx++)
    {
        /* print the current history item */
        printf ("\nhistoryBuff[%i] = %s\n", loopIdx, *historyBuff[loopIdx]);
        fflush(stdout);
    }
}

I'm passing my char** into the function like this:

printHistory (&commandArray);

As it stands now, everything compiles fine, but when the program prints the history, the function hangs somewhere in the loop and does not print out what is in the char**. SO, my question to you is this: Am I passing commandArray properly, am I declaring the function correctly, and am I dereferencing it the right way in the function?

Thank you in advance for any and all help or suggestions!

-Ben

+9  A: 
  1. To make your code work the way it is, you should dereference like this:

    (*historyBuff)[loopIdx]

    The way you have written things, [] happens before * because of operator precedence in C, and it's not what you want.

  2. You need allocate more space for your command array. Right now it isn't actually big enough to hold historySize char*'s:

    char** commandArray = (char**)malloc(historySize * sizeof(char*));
    
  3. You don't need to pass this array by "reference". You can just declare your function like this:

    void printHistory(char** historyBuff)
    

    And pass in commandArray directly. You would only need to pass in a char*** if you intend to change the actual array pointer somewhere in the function (e.g. if you needed to realloc it to make more space).

  4. For a function that only prints things, you could go a little further and declare things const. This is a "guarantee" to the caller (insofar as you can guarantee anything in C) that you're not going to modify the array or the strings in it:

    void printHistory(const char *const * historyBuff)
    
tgamblin
Thank you both for your help! The function works excellently now. Again, I appreciate it immensely.
BReeves
+1  A: 

1.

malloc allocates a number of bytes, not pointers. If historySize is the number of character pointers you are allocating, you will need to change:

char** commandArray = (char**)malloc(historySize);

to:

char** commandArray = malloc( historySize * sizeof(char*) );

2.

Your printHistory() doesn't change the commandArray pointer. You don't need to pass a char***. A char** will do.

Shmoopty