943,795 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 321
  • C++ RSS
Feb 8th, 2009
0

problems with new

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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):

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Feb 8th, 2009
0

Re: problems with new

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 8th, 2009
0

Re: problems with new

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
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Feb 8th, 2009
1

Re: problems with new

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: double is not large enough, what to do - pow(x,y)
Next Thread in C++ Forum Timeline: Frequency of array numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC