【问题标题】:sccz80:"../lib/main.c" L:16 Warning:#14:Expected ',' sccz80:"../lib/main.c" L:16 Error:#28:Segmentation faultsccz80:"../lib/main.c" L:16 警告:#14:Expected ',' sccz80:"../lib/main.c" L:16 错误:#28:分段错误
【发布时间】:2019-12-24 17:53:54
【问题描述】:

编译以下代码时出现以下错误:

sccz80:"../lib/main.c" L:16 Warning:#14:Expected ','
sccz80:"../lib/main.c" L:16 Error:#28:Segmentation fault
/*
 * A test game of Pong using the Z88dk
 */

#include <spectrum.h>
#include <graphics.h>
#include <sprites/sp1.h>
#include <stdio.h>

struct Bat {
    int x;
    int y;
    int w;
    int h;
};

void clear_screen(Bat* bat)
{
    undrawb(bat.x, bat.y, bat.w, bat.h);
}

int main()
{
    struct Bat bat;
    bat.x = 0;
    bat.y = 0;
    bat.w = 8;
    bat.h = 24;

    while(1)
    {
        zx_border(INK_GREEN);
        clear_screen(&bat);
        drawb(bat.x, bat.y, bat.w, bat.h);
    }
    return 0;
}

关于可能是什么问题的任何建议?我正在使用 z88dk 创建一个测试 ZX Spectrum 程序。不幸的是,我没有足够高的分数来添加“z88dk”标签。对此深表歉意。

【问题讨论】:

  • 尝试预处理它,看看你得到了什么; h 变量的某些内容似乎让编译器感到困惑。
  • @LightnessRacesBY-SA3.0 你介意让我快速了解一下你的意思吗?我不确定我需要定义什么。
  • 在函数clear_screen 中使用未定义的类型Bat。然后你定义了一个指针bat,但是当你访问那个未知结构的字段时,你使用.而不是-&gt;。这无法编译。
  • @Gerhardh 谢谢,那行得通。我真的需要在 Atom 中安装一些语法检查。
  • @Gerhardh 奇怪的编译器对此进行了段错误

标签: c linux zxspectrum


【解决方案1】:

您的程序中有 2 个错误:

void clear_screen(Bat* bat)
{
    undrawb(bat.x, bat.y, bat.w, bat.h);
}

您的代码中没有定义类型Bat。只有struct Bat。 那么bat 的类型是“指向结构的指针”。这意味着您无法使用 . 运算符访问结构成员,但您需要通过 -&gt; 取消引用。

很奇怪,你的编译器提供了一条错误消息,其中不包含任何这些错误,而是提到了一行(假设L:16 表示第 16 行)以及与代码不匹配的某些原因。

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多