C++ static character array...

Discussion in 'C++' started by John Ratliff, Jan 27, 2004.

  1. John Ratliff

    John Ratliff Guest

    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
    John Ratliff, Jan 27, 2004
    #1
    1. Advertising

  2. John Ratliff wrote in news:bv6gis$ijm$:

    > 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"
    > }


    ;

    >


    With the missing ; added this compiles fine, what is your error message.

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


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jan 27, 2004
    #2
    1. Advertising

  3. John Ratliff

    John Ratliff Guest

    -- 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 << 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" <> wrote in message
    news:Xns947DCFDD950D5ukcoREMOVEfreenetrtw@195.129.110.131...
    > John Ratliff wrote in news:bv6gis$ijm$:
    >
    > > 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"
    > > }

    >
    > ;
    >
    > >

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

    >
    > Rob.
    > --
    > http://www.victim-prime.dsl.pipex.com/
    John Ratliff, Jan 29, 2004
    #3
  4. John Ratliff

    Ron Natalie Guest

    "John Ratliff" <> wrote in message news:...

    >
    > What is the difference between
    >
    > const char **var and const char *var[]?
    >

    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.
    Ron Natalie, Jan 29, 2004
    #4
  5. John Ratliff

    John Ratliff Guest

    "Ron Natalie" <> wrote in message
    news:40193a14$0$234$...
    >
    > "John Ratliff" <> wrote in message

    news:...
    >
    > >
    > > What is the difference between
    > >
    > > const char **var and const char *var[]?
    > >

    > 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.


    Thanks.

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

    ---
    "I'm learnding." (--Ralph Wiggum)
    John Ratliff, Jan 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,758
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    890
    raavi
    Mar 2, 2006
  3. cgbusch
    Replies:
    6
    Views:
    7,454
    Mike Brown
    Sep 2, 2003
  4. mimmo
    Replies:
    4
    Views:
    27,920
  5. KwikRick
    Replies:
    1
    Views:
    349
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
Loading...

Share This Page