problems with new

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

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

problems with new

 
0
  #1
Feb 8th, 2009
I'm trying to use new but I can't seem to get it right...

in this example I ask how many strings the user would want to input, then ask for each one, then echo them back.

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char** argv){
  6.  
  7. int num, i;
  8. char** a;
  9.  
  10. cout<< "how many strings? ";
  11. cin>> num;
  12. *a = new char[num];
  13.  
  14. for(i=0;i<num;i++){
  15. cout<< "enter a string: ";
  16. cin>> a[i];
  17. }
  18. for(i=0;i<num;i++){
  19. cout<< "string "<< i<< ": "<< a[i]<< endl;
  20. }
  21.  
  22. return 0;
  23.  
  24. }

I wind up with this (in linux):

  1. how many strings? 3
  2. enter a string: hello
  3. enter a string: there
  4. Segmentation Fault

I tried moving the *s around, putting them on a[i], taking them back off, etc. nothing has worked... All it did was change when the segfault happened. What am I doing wrong?

--EDIT--

I also tried *a = new char[num][80] and *a = new *char[num] to no avail.
If I use what is written above, and only choose to write 1 string, it works fine.
Last edited by winrawr; Feb 8th, 2009 at 3:36 pm. Reason: addendum
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problems with new

 
0
  #2
Feb 8th, 2009
why is 'a' a pointer to a pointer?

edit: Hmmm...I see what's you're trying to do.
Last edited by Comatose; Feb 8th, 2009 at 3:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: problems with new

 
0
  #3
Feb 8th, 2009
I have no idea... I just thought **a was the same as a[][], like a two dimensional character array, an array of strings, etc.
I'm not sure what I did wrong, I'm not totally literate with using pointers yet
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: problems with new

 
1
  #4
Feb 8th, 2009
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char** argv){
  6.  
  7. int num, i;
  8. char** a;
  9.  
  10. cout<< "how many strings? ";
  11. cin>> num;
  12. a = new char*[num];
  13.  
  14. for(i=0;i<num;i++){
  15. a[i] = new char[80];
  16. cout<< "enter a string: ";
  17. cin>> a[i];
  18. }
  19.  
  20. for(i=0;i<num;i++)
  21. cout<< "string "<< i<< ": "<< a[i]<< endl;
  22.  
  23. return 0;
  24. }
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC