Hi there! So I've never programmed before and I'm trying to teach myself some C++ before I get back to school and dive headfirst into Java. I'm going through the MIT C++ OpenCourseware and I'm like 4 days in. I decided to go back and try to test myself on arrays and I crashed my first program! (YAY! -.-) It compiles correctly and it works up until line 17/18 (Where I take the input and store it in p, then try to store p in the array). At that point, the program crashes no matter what input I give it and I can't figure out why.
#include <iostream> // "
#include <string> // "
#include <cstring> // "
// all dat jazz
usingnamespace std; // "
// "
int main () { // "
int x; // declaring x
int y = 0; // declaring y for the for loop
char *p; // declaring p, which I'm gonna use for storing values into the array cause it wont compile if I directly input the input into the array for some reason
cout << "How large would you like the array to be?: "; // self-explanatory
cin >> x; // takes the desired array size
for (; y < x; ++y) { // for loop, incremented by 1 between 0 and x
char *array1[x] ; // declaring the array as a character array
cout << "String " << y+1 << ": "; // aesthetics
cin >> p; // takes the input and stores it as p
array1 [y] = p; // takes p and stores it in the appropriate slot in the array (Here's where it crashes)
return 1; // done (but I don't actually ever get here)
}
}
Like I said, this is all completely new to me so I don't know how to debug things yet. Any and all help will be appreciated, even if it isn't exactly related to the topic (like if I should use different naming practices or order things differently). And be as mean as you want to be! I can take it!
Thanks in advance for the help!
P.S. I'm using the Code::Blocks IDE, if that makes a difference.
When using g++ to compile your code you should use the -pedantic and -wall options at a minimum.
You will probably want to also use -std=c++14
Line 15 is not legal C++ (and I doubt you wanted an array of pointers anyway.)
Line 17 tries to read into the memory at whatever random place the uninitialized pointer p happens to be pointing, resulting in undefined behavior.
So I think I'm starting to get it. So when I use the asterisk, that means I'm defining a pointer and not a variable? Although I haven't found out what g++ is (I'll do some quick searching), and I don't know how to implement the -std=c++14 into my code (Although I have run into the situation where it tells me to use it). Do I just put it in line at the start? And (sorry if this is a lot of questions) what do -pedantic and -wall do?
Edit: So I found out what g++ is. Now I just need to figure out how to implement it.
Edit 2: I've gotten it to not crash now. I just have to figure out how to print it. I feel like I didn't do it the way you said I should though. I just moved my array out of the for loop and changed the array's and p's variable type from char to string.
If you aren't invoking the compiler directly, the IDE you're working with will have some settings you can change (or that it will pass on to the compiler) which should have the same effect.
I feel like I didn't do it the way you said I should.
I didn't tell you how you should do it. I'm not entirely sure what you're trying to do.
[edit]
This would be how I recommend you do what I think you're trying to do: