double pointer indirection??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nobody

    double pointer indirection??

    excuse the "windows" code below, but this is a C++ question... or maybe a C.

    Anyways, I want a function where I can pass in an array of strings (variable
    count, variable size), so I defined this:

    void Test(LPCTSTR* ppsz, int nCount)

    {

    for (int nIndex = 0; nIndex < nCount; nIndex++)

    TRACE(".%s.\n", ppsz[nIndex]);

    }

    and call it like:

    TCHAR sz[5][32] =

    {

    "United States",

    "Canada",

    "Mexico",

    "United Kingdom",

    "South America"

    };

    Test((LPCTSTR*) &sz, 5);



    The Test function crashes no matter what I do (various indirection styles,
    etc) even on nIndex = 0?!?!?!

    How can I do this? Would the Test function need to know the 32 to get the
    next pointer...

    Seems like what I want to do is pass in an array of pointers?

    Any simple way to do that similar to the above? without new and delete and
    so forth and not using a string list class?

    this is for an API that I am writing, so I want minimal dependencies and
    minimum chance for a developer to pass me a wrong format.

    Thanks.




  • Karl Heinz Buchegger

    #2
    Re: double pointer indirection??



    Nobody wrote:[color=blue]
    >
    > excuse the "windows" code below, but this is a C++ question... or maybe a C.
    >
    > Anyways, I want a function where I can pass in an array of strings (variable
    > count, variable size), so I defined this:
    >
    > void Test(LPCTSTR* ppsz, int nCount)
    >
    > {
    >
    > for (int nIndex = 0; nIndex < nCount; nIndex++)
    >
    > TRACE(".%s.\n", ppsz[nIndex]);
    >
    > }
    >
    > and call it like:
    >
    > TCHAR sz[5][32] =
    >
    > {
    >
    > "United States",
    >
    > "Canada",
    >
    > "Mexico",
    >
    > "United Kingdom",
    >
    > "South America"
    >
    > };
    >
    > Test((LPCTSTR*) &sz, 5);
    >
    > The Test function crashes no matter what I do (various indirection styles,
    > etc) even on nIndex = 0?!?!?!
    >
    > How can I do this? Would the Test function need to know the 32 to get the
    > next pointer...[/color]

    :-)

    You function expects something like this:

    ppsz
    +------+ +-----+
    | o-------->| o---------------> "Text1"
    +------+ +-----+
    | o---------------> "Text2"
    +-----+
    | o---------------> "Text3"
    +-----+
    | |
    . .
    . .
    +-----+

    But your sz Array looks like this

    sz
    +---+---+---+---+---+- ... 26 chars .. -+---+---+---+---+- .... -+---+---+---+---+- ...
    | U | n | i | t | e | | | C | a | n | | M | e | x | i |
    +---+---+---+---+---+- ............... -+---+---+---+---+- .... -+---+---+---+---+- ...

    As you can see, those 2 data structures are incompatible.
    [color=blue]
    >
    > Seems like what I want to do is pass in an array of pointers?[/color]

    Ok. But then you need to build up an array of pointers.

    char sz[5][32]

    is *not* an array of pointers. It is a flat memory field with a size of 5*32 bytes,
    where the index arithmetic divides the memory into the fields.
    [color=blue]
    >
    > Any simple way to do that similar to the above?[/color]

    char* sz[] = { "United States", "Canada", "Mexico" }

    But note: The pointer point to constant strings! You can't change the strings!
    [color=blue]
    > without new and delete and
    > so forth and not using a string list class?
    >
    > this is for an API that I am writing, so I want minimal dependencies and
    > minimum chance for a developer to pass me a wrong format.[/color]

    Drop the idea of passing an array of pointers to character or a 2-dimensional
    character array. As you have seen it's a constant source of confusion. If you
    got confused, so will the user of your API.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Peter van Merkerk

      #3
      Re: double pointer indirection??

      > excuse the "windows" code below, but this is a C++ question... or
      maybe a C.[color=blue]
      >
      > Anyways, I want a function where I can pass in an array of strings[/color]
      (variable[color=blue]
      > count, variable size), so I defined this:
      >
      > void Test(LPCTSTR* ppsz, int nCount)
      > {
      > for (int nIndex = 0; nIndex < nCount; nIndex++)
      > TRACE(".%s.\n", ppsz[nIndex]);
      > }
      >
      > and call it like:
      >
      > TCHAR sz[5][32] =
      > {
      > "United States",
      > "Canada",
      > "Mexico",
      > "United Kingdom",
      > "South America"
      > };
      >
      > Test((LPCTSTR*) &sz, 5);
      >
      > The Test function crashes no matter what I do (various indirection[/color]
      styles,[color=blue]
      > etc) even on nIndex = 0?!?!?![/color]

      That is not surprising considering that you pass an array of char array
      to a function that expect a pointer to char pointer. Since the character
      array is interpreted as a pointer all kinds of nasty things may happen.
      [color=blue]
      > How can I do this? Would the Test function need to know the 32 to get[/color]
      the[color=blue]
      > next pointer...[/color]

      One way to fix the problem is to make sz[] an array of const char
      pointers:

      const char* sz[] =
      {
      "United States",
      "Canada",
      "Mexico",
      "United Kingdom",
      "South America"
      };

      --
      Peter van Merkerk
      peter.van.merke rk(at)dse.nl



      Comment

      • Daniel Schüle

        #4
        Re: double pointer indirection??

        > TCHAR sz[5][32] =[color=blue]
        >
        > {
        >
        > "United States",
        >
        > "Canada",
        >
        > "Mexico",
        >
        > "United Kingdom",
        >
        > "South America"
        >
        > };
        >
        > Test((LPCTSTR*) &sz, 5);[/color]

        why not

        void print1(const char (* const x)[32])
        {
        for(int i=0; i<5; i++)
        printf("%s\n", x[i]);
        }

        //or ;)

        void print2(const char (* x)[32])
        {
        for(int i=0; i<5; i++)
        printf("%s\n", *x++);
        }

        int main()
        {
        char text[5][32] = {"a1", "b2", "c3", "d4", "e5"};
        print1(&text[0]);
        print2(&text[0]);
        return 0;
        }



        Comment

        • EventHelix.com

          #5
          Re: double pointer indirection??

          I think the problem is due to the sz declaration. This is not declaring
          an array of pointers. It is a two dimensional array of characters.

          Try declaring an array of LPCTSTR with the
          same initialization list.
          [color=blue]
          > TCHAR sz[5][32] =[/color]

          Sandeep
          --
          Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

          EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF

          Comment

          • Nobody

            #6
            Re: double pointer indirection??

            that did the trick. thanks.

            "Peter van Merkerk" <merkerk@deadsp am.com> wrote in message
            news:boq7ri$1fi tqd$1@ID-133164.news.uni-berlin.de...[color=blue][color=green]
            > > excuse the "windows" code below, but this is a C++ question... or[/color]
            > maybe a C.[color=green]
            > >
            > > Anyways, I want a function where I can pass in an array of strings[/color]
            > (variable[color=green]
            > > count, variable size), so I defined this:
            > >
            > > void Test(LPCTSTR* ppsz, int nCount)
            > > {
            > > for (int nIndex = 0; nIndex < nCount; nIndex++)
            > > TRACE(".%s.\n", ppsz[nIndex]);
            > > }
            > >
            > > and call it like:
            > >
            > > TCHAR sz[5][32] =
            > > {
            > > "United States",
            > > "Canada",
            > > "Mexico",
            > > "United Kingdom",
            > > "South America"
            > > };
            > >
            > > Test((LPCTSTR*) &sz, 5);
            > >
            > > The Test function crashes no matter what I do (various indirection[/color]
            > styles,[color=green]
            > > etc) even on nIndex = 0?!?!?![/color]
            >
            > That is not surprising considering that you pass an array of char array
            > to a function that expect a pointer to char pointer. Since the character
            > array is interpreted as a pointer all kinds of nasty things may happen.
            >[color=green]
            > > How can I do this? Would the Test function need to know the 32 to get[/color]
            > the[color=green]
            > > next pointer...[/color]
            >
            > One way to fix the problem is to make sz[] an array of const char
            > pointers:
            >
            > const char* sz[] =
            > {
            > "United States",
            > "Canada",
            > "Mexico",
            > "United Kingdom",
            > "South America"
            > };
            >
            > --
            > Peter van Merkerk
            > peter.van.merke rk(at)dse.nl
            >
            >
            >[/color]


            Comment

            Working...