| | |
problems with new
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
I wind up with this (in linux):
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
If I use what is written above, and only choose to write 1 string, it works fine.
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)
#include<iostream> using namespace std; int main(int argc, char** argv){ int num, i; char** a; cout<< "how many strings? "; cin>> num; *a = new char[num]; for(i=0;i<num;i++){ cout<< "enter a string: "; cin>> a[i]; } for(i=0;i<num;i++){ cout<< "string "<< i<< ": "<< a[i]<< endl; } return 0; }
I wind up with this (in linux):
C++ Syntax (Toggle Plain Text)
how many strings? 3 enter a string: hello enter a string: there 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
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
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'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
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
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main(int argc, char** argv){ int num, i; char** a; cout<< "how many strings? "; cin>> num; a = new char*[num]; for(i=0;i<num;i++){ a[i] = new char[80]; cout<< "enter a string: "; cin>> a[i]; } for(i=0;i<num;i++) cout<< "string "<< i<< ": "<< a[i]<< endl; return 0; }
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- If i use an illegal copy of windows,will it cause problems? (Windows NT / 2000 / XP)
- Internet explorer problems (Web Browsers)
- Problems Downloading Windows XP (Windows NT / 2000 / XP)
- Connection Problems (Networking Hardware Configuration)
- Pop up ads (Web Browsers)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: double is not large enough, what to do - pow(x,y)
- Next Thread: Frequency of array numbers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






