How to loop through an array of char pointers?

error-re&%
Last edited on
It would have been helpful if you had posted your declaration for HuffmanCodec.

Assuming message is 7-bit ASCII, I would expect the codebook array to contain 128 strings.
You have the right idea on addressing the member string of codebook. What you're missing is building up encoded.

Based on what you have, here's what I would do:
1
2
3
4
5
6
7
8
9
10
11
12
// Return a cstring containing the encoded message
char *encode (HuffmanCodec *h, const char message[])	
{   char * encoded = new char[1500];
     const char * key;

    encoded[0] = 0;  // terminator	
    for (int i = 0; message[i] != '\0'; i++)	
   {  key = h->codebook [message[i]];   // point to the key for the char in the message
       strcat (encoded, key);  //  Append the key to the encoded string
    }
    return encoded;
}

Topic archived. No new replies allowed.