A Simple Question About C++ Arrays
Translated from Chinese by Claude. Read the original
Yesterday I actually got stumped by a dead-simple C++ question. Here it is. The question:
1 | 设有下定义的语句: [c]int a3={10,20,30,40,50,60}; int b3={10,20,30,40,50,60};[/c] 则a1*b2的结果为() |
As it turned out, I just couldn’t work it out. I mixed up the declared size with the index (something like that, anyway)~ Let me explain. For example: a[x][y]={}; x and y only tell the computer how much memory to allocate. They aren’t addresses, and they don’t count from 0, whereas the positions of the data are counted by address. For example, if you fill in numbers for a[2][3]={}, the addresses are a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] and not a[1][1] a[1][2] a[1][3] a[2][1] a[2][2] a[2][3] Remember that the computer counts from 0 in memory, and that’s basically true across the whole C family. So the index and the allocated size are counted differently. Here’s a screenshot of me checking it in VC++
The answer is C) 2400. Here’s the code I used to check it:
1 |
|
If anything here is wrong, please point it out~