C语言 C 二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6107242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:44:35  来源:igfitidea点击:

C Two-Dimensional Array

cmultidimensional-arrayturbo-c

提问by aer

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?

我要问一些关于我下面提供的代码的问题......我的问题是在有评论的行中/*This line*/。我使用变量 y 和 x:y 表示高度,x 表示宽度。我第一次运行程序时,代码是scanf("%d,%d", &y, &x);但不幸的是,程序运行不正常。但是当我用 this 修改代码时scanf("%d,%d", &x, &y);,我能够依次运行程序。我无法理解它是如何发生的,因为我将 y 设置为 HEIGHT,将 x 设置为 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

回答by Baltasarq

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 correctlypoint to row 2 (third row) and column 5 (sixth column).

问题是输入数据。5,2 表示第 5 行第 2 列,问题是第 5 行不存在(高度为 5,因此您有 0 到 4 行)。一旦您更改了这些值,它们就会变成 2、5,它们正确地指向第 2 行(第三行)和第 5 列(第六列)。

回答by Pushpendra

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.

如果您对 y[HEIGHT] 和 x[WIDTH] 使用 5 和 2,它将如何工作,因为您的敌人 [HEIGHT][WIDTH] 数组是 5x10 的数组。

That is when you are using 5 for y that exceeds it limit that is 0 to 4.

那就是当您使用 5 表示超过 0 到 4 的限制时。

Isn't it.....?

是不是……?