Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

Array of arrays of string correct declaration

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

Upvotes: 0

Views: 78

Answers (2)

Hao Luong
Hao Luong

Reputation: 102

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]}}
};

Upvotes: 1

Dan H
Dan H

Reputation: 626

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

Upvotes: 0

Related Questions