C 二维数组
我要问一些关于下面提供的代码的问题...我的问题在有注释 /*This line*/
的行中。我使用变量 y 和 x:y 表示高度,x 表示宽度。我第一次运行程序时,代码是 scanf("%d,%d", &y, &x);
但不幸的是,程序运行不正常。但是当我用这个 scanf("%d,%d", &x, &y);
修改代码时,我就能够依次运行程序了。我不明白这是怎么发生的,因为我将 y 设置为高度,x 设置为宽度?
File Edit Run Compile Project Options Debug Break/watch
╒════════════════════════════════════ Edit ════════════════════════════════════╕
│ Line 1 Col 43 Insert Indent Tab Fill Unindent * C:NONAME.C │
│#define HEIGHT 5 │
│#define WIDTH 10 │
│ │
│char enemy[HEIGHT][WIDTH]= │
│ { {0,0,0,0,0,0,0,0,0,0}, │
│ {0,1,1,0,0,1,0,0,0,0}, │
│ {0,0,0,1,0,1,0,1,1,0}, │
│ {0,0,0,0,0,0,0,0,1,1}, │
│ {0,0,1,1,0,1,0,0,0,1} }; │
│ │
│main() │
│{ │
│ char friend[HEIGHT][WIDTH]; │
│ int x,y; │
│ │
│ clrscr(); │
│ │
│ for(y=0; y<HEIGHT; y++) |
| for(x=0; x<WIDTH; x++) |
| friend[y][x]='.'; |
| |
| while(x >= 0) |
| { |
| for(y=0; y<HEIGHT; y++) |
| { |
| for(x=0; x<WIDTH; x++) |
| printf("%c", friend[y][x]); |
| printf("\n"); |
| } |
| |
| printf("Coordinates: "); |
| scanf("%d,%d", &x, &y); /*This line*/ |
| |
| if(enemy[y][x] == 1) |
| friend[y][x]="\xDB"; |
| else |
| friend[y][x]="\xB0"; |
| } |
|} │
├─────────────────────────────────── Watch ────────────────────────────────────┤
│ │
└──────────────────────────────────────────────────────────────────────────────┘
F1-Help F5-Zoom F6-Switch F7-Trace F8-Step F9-Make F10-Menu NUM
I'm going to ask something about my code provided below... My question is in the line where there's a comment /*This line*/
. I used variable y and x: y for the HEIGHT and x for the WIDTH. The very first time I run the program, the code was scanf("%d,%d", &y, &x);
but unfortunately, the program was not running properly. But when I modified the code with this scanf("%d,%d", &x, &y);
, then, I was able to run the program in turn. I can't understand how it happened, since I had set the y as HEIGHT and x as WIDTH?
File Edit Run Compile Project Options Debug Break/watch
╒════════════════════════════════════ Edit ════════════════════════════════════╕
│ Line 1 Col 43 Insert Indent Tab Fill Unindent * C:NONAME.C │
│#define HEIGHT 5 │
│#define WIDTH 10 │
│ │
│char enemy[HEIGHT][WIDTH]= │
│ { {0,0,0,0,0,0,0,0,0,0}, │
│ {0,1,1,0,0,1,0,0,0,0}, │
│ {0,0,0,1,0,1,0,1,1,0}, │
│ {0,0,0,0,0,0,0,0,1,1}, │
│ {0,0,1,1,0,1,0,0,0,1} }; │
│ │
│main() │
│{ │
│ char friend[HEIGHT][WIDTH]; │
│ int x,y; │
│ │
│ clrscr(); │
│ │
│ for(y=0; y<HEIGHT; y++) |
| for(x=0; x<WIDTH; x++) |
| friend[y][x]='.'; |
| |
| while(x >= 0) |
| { |
| for(y=0; y<HEIGHT; y++) |
| { |
| for(x=0; x<WIDTH; x++) |
| printf("%c", friend[y][x]); |
| printf("\n"); |
| } |
| |
| printf("Coordinates: "); |
| scanf("%d,%d", &x, &y); /*This line*/ |
| |
| if(enemy[y][x] == 1) |
| friend[y][x]="\xDB"; |
| else |
| friend[y][x]="\xB0"; |
| } |
|} │
├─────────────────────────────────── Watch ────────────────────────────────────┤
│ │
└──────────────────────────────────────────────────────────────────────────────┘
F1-Help F5-Zoom F6-Switch F7-Trace F8-Step F9-Make F10-Menu NUM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是输入数据。 5,2 表示第 5 行、第 2 列,问题是第 5 行不存在(高度为 5,因此有从 0 到 4 的行)。一旦您更改了这些值,它们就变成了 2、5,这正确指向第 2 行(第三行)和第 5 列(第六列)。
The problem is the input data. 5,2 meant row 5, column 2, and the problem is that row 5 does not exist (Height is 5, so you have rows from 0 to 4). As soon as you changed the values, they became 2, 5, which correctly point to row 2 (third row) and column 5 (sixth column).
如果您对 y[HEIGHT] 和 x[WIDTH] 使用 5 和 2,它将如何工作,因为您的敌人 [HEIGHT][WIDTH] 数组是 5x10 的数组。
也就是说,当您对 y 使用 5 时,它超出了 0 到 4 的限制。
不是……?
If you are using 5 and 2 for y[HEIGHT] and x[WIDTH], How it will work because your enemy[HEIGHT][WIDTH] array is the array of 5x10.
That is when you are using 5 for y that exceeds it limit that is 0 to 4.
Isn't it.....?