A "How do you " question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 8
Reputation: zprise is an unknown quantity at this point 
Solved Threads: 0
zprise zprise is offline Offline
Newbie Poster

A "How do you " question

 
0
  #1
Jan 21st, 2008
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
Last edited by zprise; Jan 21st, 2008 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A "How do you " question

 
0
  #2
Jan 21st, 2008
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
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: zprise is an unknown quantity at this point 
Solved Threads: 0
zprise zprise is offline Offline
Newbie Poster

Re: A "How do you " question

 
0
  #3
Jan 21st, 2008
What do you meen "my own realloc"
And please how do you declare and define the pointer.
The program needs to have a main and functions
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A "How do you " question

 
0
  #4
Jan 21st, 2008
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
Last edited by iamthwee; Jan 21st, 2008 at 4:53 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A "How do you " question

 
0
  #5
Jan 21st, 2008
Originally Posted by iamthwee View Post
Just use a vector of strings.
I would normally agree but I don't think the OP has that option. This may be an assignment that teaches pointers, not vectors.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A "How do you " question

 
0
  #6
Jan 21st, 2008
Originally Posted by Ancient Dragon View Post
I would normally agree but I don't think the OP has that option. This may be an assignment that teaches pointers, not vectors.
Ah yes, you are probably right.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A "How do you " question

 
0
  #7
Jan 21st, 2008
Originally Posted by zprise View Post
What do you meen "my own realloc"
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.

Originally Posted by zprise View Post
And please how do you declare and define the pointer.
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.

Originally Posted by zprise View Post
The program needs to have a main and functions
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: zprise is an unknown quantity at this point 
Solved Threads: 0
zprise zprise is offline Offline
Newbie Poster

Re: A "How do you " question

 
0
  #8
Jan 21st, 2008
you're a very witty dragon.

I ment to ask how do you send or get double pointer (if that's what it's called) from function to main.
i.e.
the main has an option of sending the pointer to get strings into it and has the option of sending the pointer to have strigs deleted.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A "How do you " question

 
0
  #9
Jan 21st, 2008
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.

  1. char **foo( char*** array, size_t size)
  2. {
  3. *array = new char*[size];
  4. }
  5.  
  6. int main()
  7. {
  8. char **array = 0;
  9. foo( &array, 10 );
  10. }

Or use the c++ reference operator &, which simplifies all those asterisks
  1. char **foo( char**& array, size_t size)
  2. {
  3. array = new char*[size];
  4. }
  5.  
  6. int main()
  7. {
  8. char **array = 0;
  9. foo( array,10 );
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC