0

I need an array of arrays of string in my programm. I am declaring it so:

string edges[N][N] = {
{"0", "A", "0", "B", "E", "0", "0", "P1", "0"},
{"A", "0", "D", "I", "0", "0", "0", "0", "0"},
{"0", "D", "0", "0", "0", "H", "F", "0", "0"},
{"B", "I", "0", "0", "0", "H", "0", "0", "0"},
{"E", "0", "0", "0", "0", "0", "0", "P2", "0"},
{"0", "0", "H", "H", "0", "0", "0", "0", "P4"},
{"0", "0", "F", "0", "0", "0", "0", "0", "P3"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0"},
};

Its okay, when I am trying to access strings with 1-letter value (A or B etc), but If I try to access string with 2-letters value, like edges[0, 7] (value is "P1"), programm will output 0. What's wrong?
It working fine if element is 1-letter like A, B, C, etc, but fail with P1 or P2.
Full programm listing and working programm is here http://ideone.com/ZMiVPE

7
  • "strings which are chars"? There is no such thing. Do you means strings that are only one character long (not counting the null terminator)? Commented Jan 17, 2014 at 1:22
  • Don't you mean edges[0][7]? edges[0, 7] is the same as edges[7]. Commented Jan 17, 2014 at 1:24
  • @Angew I meant edges[0][7]. Sorry. @ValekHalfHeart, sorry for my bad english, I eddited my question. Commented Jan 17, 2014 at 1:25
  • 1
    Your counting is off... remember that the first element in an array is [0], so edges[0][8] in an array of [9][9] is the last entry on the first line... "0". The same thing in your program on ideone... the indices you're printing are legitimately "0" elements in your array. See ideone.com/OywUMI Commented Jan 17, 2014 at 1:27
  • 1
    In your edited code, cout << " -(" << edges[next][prev] << ")-> " << names[tmp] << " : " << prev << " : " << next; <-- note you print prev before next, but you index in the opposite order. If you look up [next][prev] e.g. [8][5] you'll find they are "0". Commented Jan 17, 2014 at 1:37

2 Answers 2

1

He Here's show value 0 in position [0,8], so, it's correct. well, it's correct. Array's going to arrange follow:

string edges[N][N] = {
{[0,0], [0,1], [0,2], [0,3], [0,4], [0,5], [0,6], [0,7], [0,8]},
  ....
[8,0], [8,1], [8,2], [8,3], [8,4], [8,5], [8,6], [8,7], [8,8]}}
};
Sign up to request clarification or add additional context in comments.

Comments

0

Because you want 7, not 8. C languages us a 0 index.

1 Comment

Oops. Sure it is edges[0, 7], sorry. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.