1

I'm getting the following error on compilation of the code below:

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;
}

Any suggestions on what might be the issue? I'm using the z88dk to create a test ZX Spectrum program. Unfortunately, I don't have a high enough score to add a 'z88dk' tag. Apologies for that.

the busybee
  • 10,755
  • 3
  • 13
  • 30
A. Jetman
  • 67
  • 1
  • 5
  • Try preprocessing it and see what you get; something about the `h` variable appears to be confusing the heck out of the compiler. – Lightness Races in Orbit Dec 24 '19 at 17:57
  • @LightnessRacesBY-SA3.0 Would you mind giving me a quick snippet of what you mean? I'm unsure of what I need to define. – A. Jetman Dec 24 '19 at 18:04
  • In function `clear_screen` you use an undefined type `Bat`. Then you define a pointer `bat` but when you access the fields of that unknown struct, you use `.` instead of `->`. This cannot compile. – Gerhardh Dec 24 '19 at 19:26
  • @Gerhardh Thank you, that worked. I really need to get some syntax checking installed into Atom. – A. Jetman Dec 24 '19 at 19:50
  • @Gerhardh Strange compiler segfaulting on that though – Lightness Races in Orbit Dec 25 '19 at 01:54
  • @Gerhardh Also when you have a solution to the problem please post it in the answer section not in the comments – Lightness Races in Orbit Dec 25 '19 at 01:54
  • @LightnessRacesBY-SA3.0 as the error description does not point into the direction I mentioned, I did not expect it to be the solution but just "one more bug to fix". Therefore I didn't put it in an answer initially. – Gerhardh Dec 26 '19 at 11:05

1 Answers1

2

You have 2 errors in your program:

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

There is no type Bat defined in your code. Only struct Bat. Then bat is of type "pointer to struct". This means you cannot access the struct members using . operator but you need to dereference via ->.

It is really strange that your compiler provides an error message that does not contain any of these errors but instead mentions a line (assuming L:16 indicates line 16) and some reason that do not match the code.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39