| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"howa" |
| Date: |
22 Oct 2006 12:17:42 PM |
| Object: |
Pointer to Pointer |
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.
.
|
|
| User: "Bart" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 12:31:41 PM |
|
|
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.
.
|
|
|
| User: "howa" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 01:06:54 PM |
|
|
Bart =E5=AF=AB=E9=81=93=EF=BC=9A
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 =3D "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.
.
|
|
|
| User: "Bart" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 02:25:49 PM |
|
|
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.
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 04:18:30 PM |
|
|
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.
.
|
|
|
| User: "Bart" |
|
| Title: Re: Pointer to Pointer |
23 Oct 2006 03:16:51 AM |
|
|
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.
.
|
|
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 01:15:12 PM |
|
|
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[i] = 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.
.
|
|
|
| User: "howa" |
|
| Title: Re: Pointer to Pointer |
23 Oct 2006 09:25:13 AM |
|
|
Pete Becker =E5=AF=AB=E9=81=93=EF=BC=9A
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 =3D new str[whatever_you_need];
for (int i =3D 0; i < whatever_you_need; ++i)
str[i] =3D 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!
.
|
|
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 01:12:18 PM |
|
|
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.
.
|
|
|
| User: "Bart" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 02:18:33 PM |
|
|
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.
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 04:16:56 PM |
|
|
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.
.
|
|
|
|
|
|
|
| User: "Default User" |
|
| Title: Re: Pointer to Pointer |
22 Oct 2006 01:54:28 PM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|