CS50 PSET3游戏十五:绘制功能错误 [英] CS50 PSET3 game of fifteen: draw function error

查看:189
本文介绍了CS50 PSET3游戏十五:绘制功能错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初的绘图功能没有错误,但输出错误。为了用GDB测试draw(),我尝试为draw()创建一个单独的文件。

问题是这个运行时错误:

I originally had my draw function working with no errors, but giving wrong outputs. To test draw() with GDB, I tried making a separate file for draw().
The problem is this runtime error:

~/workspace/pset3/fifteen/ $ ./Untitled2 Untitled2.c:10:15: runtime error: variable length array bound evaluates to non-positive value 0 Untitled2.c:10:18: runtime error: variable length array bound evaluates to non-positive value 0





非常感谢,



< b>我尝试了什么:





Thanks a lot,

What I have tried:

include <cs50.h>
include <stdio.h>
include <stdlib.h>
include <unistd.h>
int main(int argc, string argv[]) {
	int d = atoi(argv[0]);
	int board[d][d];
	for (int e = 0; e < d; e++) {
		for (int f = 0; f < d; f++) {
			if (!( ( e == d-1) && ( f == d-1) )) {
				printf("%2d", board[e][f]);
			}
			if ( ( e == d-1) && ( f == d-1) )
			{
				printf("%2c", '_');
			}

		}
		printf("\n");

		printf("\n %i", argc);
	}
}

推荐答案

./ Untitled2 Untitled2.c:10:15:运行时错误:可变长度数组绑定求值为非正值0 Untitled2.c:10:18:运行时错误:可变长度数组绑定求值为非正值0
./Untitled2 Untitled2.c:10:15: runtime error: variable length array bound evaluates to non-positive value 0 Untitled2.c:10:18: runtime error: variable length array bound evaluates to non-positive value 0





非常感谢,



我尝试过:





Thanks a lot,

What I have tried:

include <cs50.h>
include <stdio.h>
include <stdlib.h>
include <unistd.h>
int main(int argc, string argv[]) {
	int d = atoi(argv[0]);
	int board[d][d];
	for (int e = 0; e < d; e++) {
		for (int f = 0; f < d; f++) {
			if (!( ( e == d-1) && ( f == d-1) )) {
				printf("%2d", board[e][f]);
			}
			if ( ( e == d-1) && ( f == d-1) )
			{
				printf("%2c", '_');
			}

		}
		printf("\n");

		printf("\n %i", argc);
	}
}


因此请使用调试器并找出值是什么:错误消息非常明确:

So use the debugger and find out what the value is: the error message is pretty explicit:
variable length array bound evaluates to non-positive value

所以d为零或负数,这意味着 atoi 返回了。那么什么是 argv [0] 评估为零或负数?



提示:第一个C应用程序运行时参数的参数是应用程序exe名称...

So d is zero or negative, which means that atoi returned that. so what is in argv[0] that evaluates to zero or a negative number?

Hint: the first parameter of a C application runtime arguments is the application exe name...


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于CS50 PSET3游戏十五:绘制功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆