Char***

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    Char***

    Im having trouble in the c++ world grasping char arrays.

    Im given a char*** from a thrid party function and i want to loop through the returned strings and just message box them.

    This is what i have:

    Code:
    	char ***lst;
    	ll_cnt = GetWords(lst);
    
    	for(int i = 0; i < ll_cnt; i++)
    	{
    		MessageBox(0, str, *(lst[i]), 0);
    	}
    It works the first time, where i = 0, but after that fails.

    What am i doing wrong?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Let's clarify that char*** variable. Do you have something similar to this:
    Code:
    char a[] = "some string";
    char *b = &a[0];
    char **c = &b;
    char ***d = &c;
    or is it more like this:
    Code:
    char a[] = "some string";
    char *b[] = { &a[0], ... };
    char **c[] = [ &b[0], ... };
    char ***d = &c[0];
    You need to know the memory organization, not just the variable types.

    Comment

    • ShadowLocke
      New Member
      • Jan 2008
      • 116

      #3
      Well i cant really figure it out..

      The function I call is defined as "int GetWords(char** * slst);"

      Though i just figured out my problem (blindly troubleshooting and playing)

      I changed:

      Code:
      *(lst[i])
      to:

      Code:
      (*lst)[i]
      and it worked!

      Thanks!

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Sounds like the memory organization is something like this ...

        Code:
        #define N <number of words>
        int ll_cnt = N;
        char *array[N] = { "string1", "string2", ..., "stringN" };
        char **pArray = array;
        char ***lst = &pArray;
        (*lst) is the value of pArray, which is the address of the array; which you index into with [i]. The result is successive char-pointers from the array.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Please read this article: http://bytes.com/topic/c/insights/77...rrays-revealed.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by ShadowLocke
            Code:
            	char ***lst;
            	ll_cnt = GetWords(lst);
            This looks wrong to me.

            If you have a function that takes a char *** parameter and uses it to pass back data then that function is expecting a pointer to an existing location but you do not pass a pointer to an existing location you pass the value of a pointer.

            The only place GetWords has to write to in your function is the completely random and uninitialised address you pass in contained in lst.

            Also from the name and return type it appears that the function is returning a list of words. Dynamically allocated that would be an array of pointers with each pointer pointing to allocated memory. A pointer to such an array would have type char **.

            Taking all this in mind I think the 2 code lines above should actually be

            Code:
            	char **lst;
            	ll_cnt = GetWords(&lst);
            passing the address of a char ** for the function to write the address of the array of strings to. Obviously the change of type would then also have an effect on the rest of your code.

            At the moment I believe the 2 lines highlighted actually produce undefined behaviour as you are writing to an un-allocated memory address somewhere.

            When a function takes a pointer then the majority of the time it wants the address of an allocated piece of memory or may be NULL, not an uninitialised pointer.

            Comment

            Working...