Pointer to Pointer

Discussion in 'C++' started by howa, Oct 22, 2006.

  1. howa

    howa Guest

    Hi,

    How to manually add an array of string, which is pointer by a pointer,
    e.g.

    char **str;

    // add "this", "is", "a", "test" one by one into the 2d array?

    thanks.
    howa, Oct 22, 2006
    #1
    1. Advertising

  2. howa

    Bart Guest

    howa wrote:
    > Hi,
    >
    > How to manually add an array of string, which is pointer by a pointer,


    In C++ a string means the std::string class:

    #include <string>

    std::string mystring = "this is a string";

    What you probably mean is a C-style array of chars, but it is
    recommended that you avoid them in C++.

    > char **str;
    >
    > // add "this", "is", "a", "test" one by one into the 2d array?


    You have a pointer, not a 2D array. It is very, VERY wrong to think of
    double-pointers as arrays.

    Anyway, you should use a standard vector of strings instead:

    #include <vector>
    #include <string>

    std::vector<std::string> mystrings;
    mystrings.push_back("some string");

    mystrings[0]; // <-- you access the vector just like any array

    Regards,
    Bart.
    Bart, Oct 22, 2006
    #2
    1. Advertising

  3. howa

    howa Guest

    Bart 寫é“:

    > howa wrote:
    > > Hi,
    > >
    > > How to manually add an array of string, which is pointer by a pointer,

    >
    > In C++ a string means the std::string class:
    >
    > #include <string>
    >
    > std::string mystring = "this is a string";
    >
    > What you probably mean is a C-style array of chars, but it is
    > recommended that you avoid them in C++.
    >
    > > char **str;
    > >
    > > // add "this", "is", "a", "test" one by one into the 2d array?

    >
    > You have a pointer, not a 2D array. It is very, VERY wrong to think of
    > double-pointers as arrays.
    >
    > Anyway, you should use a standard vector of strings instead:
    >
    > #include <vector>
    > #include <string>
    >
    > std::vector<std::string> mystrings;
    > mystrings.push_back("some string");
    >
    > mystrings[0]; // <-- you access the vector just like any array
    >
    > Regards,
    > Bart.


    is it possible to create dynamically using char* ?

    just like the int main(char **argv) ?

    thanks.
    howa, Oct 22, 2006
    #3
  4. howa

    Pete Becker Guest

    Bart wrote:
    > howa wrote:
    >> Hi,
    >>
    >> How to manually add an array of string, which is pointer by a pointer,

    >
    > In C++ a string means the std::string class:
    >
    > #include <string>
    >
    > std::string mystring = "this is a string";
    >


    Not when the text clearly refers to a C-style string.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 22, 2006
    #4
  5. howa

    Pete Becker Guest

    howa wrote:
    >
    > is it possible to create dynamically using char* ?
    >
    > just like the int main(char **argv) ?
    >


    Yup.

    typedef char *str; // less confusing
    str *mystr; // dynamically allocated array
    mystr = new str[whatever_you_need];
    for (int i = 0; i < whatever_you_need; ++i)
    str = new char[whatever_i_needs];

    The bookkeeping can be tricky, though, so be careful.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 22, 2006
    #5
  6. howa

    Default User Guest

    howa wrote:

    > Hi,
    >
    > How to manually add an array of string, which is pointer by a pointer,
    > e.g.
    >
    > char **str;
    >
    > // add "this", "is", "a", "test" one by one into the 2d array?
    >
    > thanks.



    Sounds like homework. What do you have so far? What confuses you? Do
    you know what dynamic memory allocation is, and how to do it?

    Crack your book and get to it!




    Brian
    Default User, Oct 22, 2006
    #6
  7. howa

    Bart Guest

    Pete Becker napisal(a):
    > Bart wrote:
    > > howa wrote:
    > >> How to manually add an array of string, which is pointer by a pointer,

    > >
    > > In C++ a string means the std::string class:

    >
    > Not when the text clearly refers to a C-style string.


    When a poster seems confused and is obviously new to the language I
    feel obligated to point them in the right direction. I still maintain
    that in the context of C++ the term 'string' by itself should be used
    only for std::string and that a more qualified term should be used to
    refer to C-style strings.

    Regards,
    Bart.
    Bart, Oct 22, 2006
    #7
  8. howa

    Bart Guest

    howa napisal(a):
    > is it possible to create dynamically using char* ?
    >
    > just like the int main(char **argv) ?


    That's not a correct declaration of main. And yes, you can allocate it
    dynamically, but the question is why? Is this homework? If it's not,
    then you should use std::string. Even the argv parameter of main should
    not be manipulated as is, but used to initialize a std::string instead.

    Regards,
    Bart.
    Bart, Oct 22, 2006
    #8
  9. howa

    Pete Becker Guest

    Bart wrote:
    > Pete Becker napisal(a):
    >> Bart wrote:
    >>> howa wrote:
    >>>> How to manually add an array of string, which is pointer by a pointer,
    >>> In C++ a string means the std::string class:

    >> Not when the text clearly refers to a C-style string.

    >
    > When a poster seems confused and is obviously new to the language I
    > feel obligated to point them in the right direction. I still maintain
    > that in the context of C++ the term 'string' by itself should be used
    > only for std::string and that a more qualified term should be used to
    > refer to C-style strings.
    >


    I see. You point them in the right direction by stating your opinion as
    if it was a universal truth.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 22, 2006
    #9
  10. howa

    Pete Becker Guest

    Bart wrote:
    > Even the argv parameter of main should
    > not be manipulated as is, but used to initialize a std::string instead.
    >


    There's nothing wrong with indexing into argv. Creating unnecessary
    objects just bloats your code.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 22, 2006
    #10
  11. howa

    Bart Guest

    Pete Becker wrote:
    > Bart wrote:
    > > Even the argv parameter of main should
    > > not be manipulated as is, but used to initialize a std::string instead.

    >
    > There's nothing wrong with indexing into argv. Creating unnecessary
    > objects just bloats your code.


    Feel free to call a single instance in your main function "bloat" if
    you like. Some prefer the added convenience.

    Regards,
    Bart.
    Bart, Oct 23, 2006
    #11
  12. howa

    howa Guest

    Pete Becker 寫é“:

    > howa wrote:
    > >
    > > is it possible to create dynamically using char* ?
    > >
    > > just like the int main(char **argv) ?
    > >

    >
    > Yup.
    >
    > typedef char *str; // less confusing
    > str *mystr; // dynamically allocated array
    > mystr = new str[whatever_you_need];
    > for (int i = 0; i < whatever_you_need; ++i)
    > str = new char[whatever_i_needs];
    >
    > The bookkeeping can be tricky, though, so be careful.
    >
    > --
    >
    > -- Pete
    >
    > Author of "The Standard C++ Library Extensions: a Tutorial and
    > Reference." For more information about this book, see
    > www.petebecker.com/tr1book.


    thanks!
    howa, Oct 23, 2006
    #12
    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. Replies:
    10
    Views:
    673
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    823
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,230
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    626
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    442
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page