Char array initialization bug in vc++ .net 2003 ?

J

Jim Langston

Complete compilable code:

#include <iostream>
#include <string>

int main()
{
char nums [ ] [3][5] = { " 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8","
9","10","11","12","13","14","15"};
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 5; j++)
std::cout << nums[j] << " ";
std::cout << std::endl;
}
std::string Wait;
std::cin >> Wait;
}

Output is:
1 2 3 4 5
4 5 6 7 8
7 8 9 10 11

Expected output would be:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

If I change it from char nums [][3][5] to int nums[3][5] and make them ints
in the initialization list, that is indeed my output.

Is this a bug in Microsoft Visual C++ .net 2003 or am I doing something
wrong?
 
B

Ben Pope

Jim said:
Complete compilable code:

#include <iostream>
#include <string>

int main()
{
char nums [ ] [3][5] = { " 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8","
9","10","11","12","13","14","15"};
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 5; j++)
std::cout << nums[j] << " ";
std::cout << std::endl;
}
std::string Wait;
std::cin >> Wait;
}

Output is:
1 2 3 4 5
4 5 6 7 8
7 8 9 10 11

Expected output would be:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

If I change it from char nums [][3][5] to int nums[3][5] and make them ints
in the initialization list, that is indeed my output.


The output is the same on VS 2005.

Switching the indices on the initialisation:
char nums [ ] [5][3] ...

"fixes" the output.

Consider the following, I think you have the order of the bounds a bit
screwy, but I could be wrong:


#include <iostream>
#include <string>

int main()
{
char nums [2] [5] [3] = {{" 1", " 2", " 3", " 4", " 5"},
{" 6", " 7", " 8", " 9", "10"}};
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 5; j++)
std::cout << nums[j] << " ";
std::cout << std::endl;
}
std::string Wait;
std::cin >> Wait;
}

Ben Pope
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Hint: typeid(nums).name() is "A5_A3_A5_c" on gcc,
i.e., 5 arrays, each with 3 arrays of 5 chars.

If you reorganize the initialization of nums:
char nums[][3][5] = { " 1", " 2", " 3",
" 4", " 5", " 6",
" 7", " 8", " 9",
"10", "11", "12",
"13", "14", "15"};
it becomes clear what is going "wrong".
Smiley: use char nums[][3][2] and try to compile,
change to char nums[][3][3] and try again.

Solution: define nums as const char* nums[3][5],
because a string literal is const char* in C++.
typeid(nums).name() now is "A3_A5_Pc",
i.e., 3 arrays, each with 5 pointers to const char.

Regards, Stephan
 
J

Jim Langston

Stephan Brönnimann said:
Hint: typeid(nums).name() is "A5_A3_A5_c" on gcc,
i.e., 5 arrays, each with 3 arrays of 5 chars.

If you reorganize the initialization of nums:
char nums[][3][5] = { " 1", " 2", " 3",
" 4", " 5", " 6",
" 7", " 8", " 9",
"10", "11", "12",
"13", "14", "15"};
it becomes clear what is going "wrong".
Smiley: use char nums[][3][2] and try to compile,
change to char nums[][3][3] and try again.

Solution: define nums as const char* nums[3][5],
because a string literal is const char* in C++.
typeid(nums).name() now is "A3_A5_Pc",
i.e., 3 arrays, each with 5 pointers to const char.

Regards, Stephan

Ahh, I think I got it. It was the initial [] that was throwing it off.
I didn't consider that. I actually never write code initialing strings that
was but was testing code for an answer to someone else's question using that
sytax and it didn't come out as expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top