C++ static character array...

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

    C++ static character array...

    How can you define a static character pointer array in C++?

    ex:

    class tmp {
    private:
    static const char *ARR[];
    };

    const char *tmp::ARR[] =
    {
    "nts1", "nts2", "nts3"
    }

    --- This doesn't compile for me, even if I switch it to use two *'s and
    abandon the [].

    ---

    However, this is valid:

    static const char *ARR[] =
    {
    "nts1", "nts2", "nts3"
    }

    This will work, but I would like to know how to embed the declaration
    inside a class.

    Thanks,

    --Dominic

  • Rob Williscroft

    #2
    Re: C++ static character array...

    John Ratliff wrote in news:bv6gis$ijm $1@hood.uits.in diana.edu:
    [color=blue]
    > How can you define a static character pointer array in C++?
    >
    > ex:
    >
    > class tmp {
    > private:
    > static const char *ARR[];
    > };
    >
    > const char *tmp::ARR[] =
    > {
    > "nts1", "nts2", "nts3"
    > }[/color]

    ;
    [color=blue]
    >[/color]

    With the missing ; added this compiles fine, what is your error message.
    [color=blue]
    > --- This doesn't compile for me, even if I switch it to use two *'s and
    > abandon the [].
    >[/color]

    Rob.
    --

    Comment

    • John Ratliff

      #3
      Re: C++ static character array...

      -- temp.cc --
      #include <iostream>

      using namespace std;

      class tc {
      private:
      static const char **ARR;
      public:
      void print();
      };

      const char **tc::ARR =
      {
      "hello", "world", "there"
      };

      void tc::print() {
      for (int i = 0; i < 3; i++) {
      cout << ARR[i] << endl;
      }
      }

      int main(int argc, char **argv) {
      tc app;

      app.print();

      return 0;
      }
      --- end of temp.cc

      g++ temp.cc
      test.cc:15: initializer for scalar variable requires one element

      What is the difference between

      const char **var and const char *var[]?

      --Dominic

      "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> wrote in message
      news:Xns947DCFD D950D5ukcoREMOV Efreenetrtw@195 .129.110.131...[color=blue]
      > John Ratliff wrote in news:bv6gis$ijm $1@hood.uits.in diana.edu:
      >[color=green]
      > > How can you define a static character pointer array in C++?
      > >
      > > ex:
      > >
      > > class tmp {
      > > private:
      > > static const char *ARR[];
      > > };
      > >
      > > const char *tmp::ARR[] =
      > > {
      > > "nts1", "nts2", "nts3"
      > > }[/color]
      >
      > ;
      >[color=green]
      > >[/color]
      >
      > With the missing ; added this compiles fine, what is your error message.
      >[color=green]
      > > --- This doesn't compile for me, even if I switch it to use two *'s and
      > > abandon the [].
      > >[/color]
      >
      > Rob.
      > --
      > http://www.victim-prime.dsl.pipex.com/[/color]


      Comment

      • Ron Natalie

        #4
        Re: C++ static character array...


        "John Ratliff" <jdr@nospam.com > wrote in message news:101idi38gm cvoa4@corp.supe rnews.com...
        [color=blue]
        >
        > What is the difference between
        >
        > const char **var and const char *var[]?
        >[/color]
        The first is a pointer to a pointer to const char.
        The second is an array (length unspecified) of pointers to const char.

        Despite much confusion on newbie programmers part and the fact that
        there is an implicit array to pointer conversion, ARRAYS and POINTERS
        ARE NOT THE SAME THING.

        const char**x = { "a", "b" };

        fails because you are trying to use an aggregate initalizer to initialize something
        that's not an aggregate. x points to exactly one thing, which is another pointer.

        const char * x[] = { "a", "b" };

        works because x is now an array of multiple things, so you can use the aggregate
        initializer to set each of them. The things inside the {} are now valid initializers
        for each const char* that is in the array.

        Comment

        • John Ratliff

          #5
          Re: C++ static character array...


          "Ron Natalie" <ron@sensor.com > wrote in message
          news:40193a14$0 $234$9a6e19ea@n ews.newshosting .com...[color=blue]
          >
          > "John Ratliff" <jdr@nospam.com > wrote in message[/color]
          news:101idi38gm cvoa4@corp.supe rnews.com...[color=blue]
          >[color=green]
          > >
          > > What is the difference between
          > >
          > > const char **var and const char *var[]?
          > >[/color]
          > The first is a pointer to a pointer to const char.
          > The second is an array (length unspecified) of pointers to const char.
          >
          > Despite much confusion on newbie programmers part and the fact that
          > there is an implicit array to pointer conversion, ARRAYS and POINTERS
          > ARE NOT THE SAME THING.
          >
          > const char**x = { "a", "b" };
          >
          > fails because you are trying to use an aggregate initalizer to initialize[/color]
          something[color=blue]
          > that's not an aggregate. x points to exactly one thing, which is[/color]
          another pointer.[color=blue]
          >
          > const char * x[] = { "a", "b" };
          >
          > works because x is now an array of multiple things, so you can use the[/color]
          aggregate[color=blue]
          > initializer to set each of them. The things inside the {} are now valid[/color]
          initializers[color=blue]
          > for each const char* that is in the array.[/color]

          Thanks.

          P.S. Sorry for top-posting. I didn't know...

          ---
          "I'm learnding." (--Ralph Wiggum)


          Comment

          Working...