>How can I create an array of strings in C/C++ ? I can use an array
>of pointers for a finite number of strings like MAXSIZE as char
>*array[MAXSIZE-1]; Can I use a pointer to pointer char **array; for
>infinite number of strings? If that is the case how will I
>initialise the char ** ?
Why can't you use a std::vector of std::string and remove all need for
tracking allocation?
Nevertheless...
>Follows the code I am trying....
>
>class DynamicStringArray
>{
>
> private:
> char **ptrptr; //I am usinng char ** instead of char *[]
> int position;
>
> public:
> DynamicStringArray()
> {
> ptrptr=0;position=0;
> }
Here you initialise your data members. Note that ptrptr is now pointing
to NULL, instead of being uninitialised. On a tangent, a better way of
doing this initialisation is...
DynamicStringArray(): ptrptr(0), position(0) {}
> void addString(char *ptr)
> {
> ptrptr[position]=new char(strlen(ptr) +1);
>//crash here
It's crashing because ptrptr is pointing at NULL. You haven't allocated
any memory for this array!
> strcpy(ptrptr[position],ptr);
> position ++;
> }
> void displayString(int data)
> {
> cout<<"String at "<<data<<"is:"<<ptrptr[data]<<endl;
> }
>
>};
>
>This program is crashing at addString() function at first line.
>
>Please help me in this regards,
But as I pointed out above, if you're using C++, you shouldn't really be
using raw arrays in 'real code'
--
PJH
"Real programmers can write assembly code in any language." - Larry Wall
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : [EMAIL PROTECTED] Service : [EMAIL PROTECTED]
This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify [EMAIL PROTECTED]
This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
| Yahoo! Groups Sponsor | |
|
|
Yahoo! Groups Links
- To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
