array of strings problem

When ever I include an array of strings, once the file compiles and links it crashes. Does anybody know the cause of this and possibly a fix?
You haven't given us any code or demonstration of your errors. How can we possibly diagnose?
ok ill give an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
      string test[0] = "this is a test";

       cout << test[0] << endl;

       return 0;

}
Well you can' t declare an array like that. An array cannot have zero elements. (At the very least that should cause some funny runtime errors but I'm not sure about whether or not the compiler has issues with a 0-element array.)
You should write code like THIS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
      string test[1];

      test[0] = "this is a test";

      cout << test[0] << endl;

      return 0;

}


You could also write it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

int main()
{
      string test[1] = {"this is a test"};

      cout << test[0] << endl;

      return 0;

}
Last edited on
wow it worked thanks. i was just using an element of zero for the test but im usung a much bigger on for the program I'm writing.
Last edited on
yea while array elements start at the zeroth element that doesn't mean when declaring you subtract one. for instance if you want 100 elements in your array you say int array[100]; which gives you elements from zeroth to ninetynineth 0-99.
Topic archived. No new replies allowed.