| | |
A "How do you " question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
Simple for others , not for me.
How do you allocate memory for an array of pointers, each pointer pointing on a string.
The problem is , that the size of the array could change depending on the number of strings the user wants to enter.
This needs to be done in a function.
There are other functions needed to be written from the main.
At least get me started
How do you allocate memory for an array of pointers, each pointer pointing on a string.
The problem is , that the size of the array could change depending on the number of strings the user wants to enter.
This needs to be done in a function.
There are other functions needed to be written from the main.
At least get me started
Last edited by zprise; Jan 21st, 2008 at 3:51 pm.
In c++ you have the choice of using either new operator or malloc() function to allocate that memory. new is preferred in c++ but malloc() may be a better solution when you need to change the size of the array while the program is running.
An array of pointers to strings that can be resized at any time is declared like this: [icode]char **array[/b]. to allocate memory for 20 strings
now, if you want to expand it to 30 strings
Similar thig can be done using new but you have to write your own realloc() function.
An array of pointers to strings that can be resized at any time is declared like this: [icode]char **array[/b]. to allocate memory for 20 strings
char **array = (char **)malloc(20 * sizeof(char **)); now, if you want to expand it to 30 strings
array = (char **)realloc(array, 30 * sizeof(char **)); Similar thig can be done using new but you have to write your own realloc() function.
Last edited by Ancient Dragon; Jan 21st, 2008 at 4:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Just use a vector of strings.
Oh and it is not a good idea to mix malloc in c++, always stick to using new and delete if you must... here's why.
http://www.informit.com/guides/conte...plus&seqNum=33
Oh and it is not a good idea to mix malloc in c++, always stick to using new and delete if you must... here's why.
http://www.informit.com/guides/conte...plus&seqNum=33
Last edited by iamthwee; Jan 21st, 2008 at 4:53 pm.
*Voted best profile in the world*
Last edited by Ancient Dragon; Jan 21st, 2008 at 5:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
c++ does not have the equivalent of the realloc() function, which allocates a new pointer, copies the data from the old pointer to the new pointer, then deletes the old pointer. And you can't use realloc() if you used new. So you would have to write your own function that does the same thing as realloc() but uses the new and delete operators.
I already showed you that in my previous post. char **array declares an array of pointers to strings, which is the purpose of the two asterisks.
Yes, all (or most) c++ programs must have a main function.
Now, start writing your program and posting code if you need more help.
I already showed you that in my previous post. char **array declares an array of pointers to strings, which is the purpose of the two asterisks.
Yes, all (or most) c++ programs must have a main function.
Now, start writing your program and posting code if you need more help.
Last edited by Ancient Dragon; Jan 21st, 2008 at 5:21 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
if you declare the pointer in main() then just pass it by reference to the other functions that need it, just as you would any other parameter.
There are two ways you can do this: pass a pointer to the array -- see the tripple stars in the declaraction. That means its a pointer to a 2d array.
Or use the c++ reference operator &, which simplifies all those asterisks
There are two ways you can do this: pass a pointer to the array -- see the tripple stars in the declaraction. That means its a pointer to a 2d array.
C++ Syntax (Toggle Plain Text)
char **foo( char*** array, size_t size) { *array = new char*[size]; } int main() { char **array = 0; foo( &array, 10 ); }
Or use the c++ reference operator &, which simplifies all those asterisks
C++ Syntax (Toggle Plain Text)
char **foo( char**& array, size_t size) { array = new char*[size]; } int main() { char **array = 0; foo( array,10 ); }
Last edited by Ancient Dragon; Jan 21st, 2008 at 5:50 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Removal of "Home Search Assistant", "Search Extender", & "Shopping Wizard" (Viruses, Spyware and other Nasties)
- Hi there... I'm the new "old" guy who stumbled into Daniweb (Community Introductions)
- OOP char [8] to char (C++)
- " ' " and post method : use of replace function ? (ASP)
- "HELP" have no sound but i got two sound cards in my pc (Windows 95 / 98 / Me)
- The "etc" folder..anyone know what'tis? (Windows NT / 2000 / XP)
- Any body who's "tired" of "trying" programs to remove anything... (Viruses, Spyware and other Nasties)
- Keyboard Issue: New IE window opens when using the "L" key. (Viruses, Spyware and other Nasties)
- OS 9.2 freez on new mem alloc ("new" opperator) (OS 7 / 8 / 9)
- dynamic memory alloc with "new" freezes OS9.2 (C++)
Other Threads in the C++ Forum
- Previous Thread: CListView - items do not display (but columns do)
- Next Thread: File mapping problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






