1

This maybe a completely obvious error but I can't figure out why my code below won't work. I'm rather unfamiliar with C, though I have made some programs using it.

I'm using Turbo C (v.3.2), so that might also be the issue; however, I'm not sure.

static struct sector
{
    float floor, ceil;
    vec2 *verts;
    signed char *neighbors;
    unsigned npoints;
}*sectors = NULL;
static unsigned NumSectors = 0;

static struct player
{
    vec3 position, velocity;
    float anlge, anglesin, anglecos, yaw;
    unsigned sector;
}player;

void DrawScreen()
{
    enum{ MaxQueue = 32 };
    unsigned s = 0;
    struct item { int sectorno,sx1, sx2; } queue[MaxQueue], *head=queue, *tail=queue;
    int ytop[W]={0}, ybottom[W], *renderedsectors = (int*)malloc(NumSectors*sizeof(int));
    for(s = 0; s < W; ++s) ybottom[s] = H-1;
    for(s = 0; s < NumSectors; ++s) renderedsectors[s] = 0;

    //This line causes an Expression syntax error.
    //From my understanding the pointer is moved to this structure inside the array.
    //Corrections welcome as I want to learn c more in order to
    //use it in more projects of mine.
    *head = (struct item) { player.sector, 0, W-1 };
    if(++head == queue+MaxQueue) head = queue;

    do{

        const struct item now = *tail;
        const struct sector* const sect = &sectors[now.sectorno];
        if(++tail == queue+MaxQueue) tail = queue;

        if(renderedsectors[now.sectorno] & 0x21) continue;
        ++renderedsectors[now.sectorno];
        for(s = 0; s < sect->npoints; ++s)
        {

        }
    }while(head != tail);
}

I'm aware of the age of Turbo C but I have to use it due to it running in DOSBox, where my final executable has to work.

I'm copying the code from this website and I'm pretty sure he used another compiler, which probably was also newer.

I didn't work a lot with C and I mostly either used C++ in my free time or C# on the job, but I need to use it now due to the restrictions I'm facing.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • sooo which line causes the error? – KamilCuk Dec 29 '22 at 20:21
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – picchiolu Dec 29 '22 at 20:49
  • @KamilCuk I have written comments that show where the error occurs and what I understand the code is supposed to do( I haven't written it myself) – Kay Steinhoff Dec 29 '22 at 21:45
  • @picchiolu It's a compiler error, if you use the same compiler(either exactly or just for the c version) as me you will recieve it – Kay Steinhoff Dec 29 '22 at 21:48
  • We can’t compile an incomplete program. Please, read what a minimal reproducible example is by following the link I provided and try to showcase the problem in the shortest possible program that still produces the error/issue you’d like us to help you with. – picchiolu Dec 29 '22 at 21:58
  • @picchiolu litteraly all you have to do is add a main() function and you are ready to go. It's a compiler error – Kay Steinhoff Dec 29 '22 at 22:05
  • 1
    @KaySteinhoff well, vec2 and vec3 don’t seem to be standard C types, so I guess my compiler will complain about those first. Instead of asking us to work around the shortcomings of your code sample, please try to provide a program that can be compiled without non-standard includes. That’s what a minimal reproducible sample is about. – picchiolu Dec 29 '22 at 22:26
  • "All you have to do is add a main() function". This is putting the burden on the wrong person. The reader shouldn't have to do anything. If you expect people to help you, you should be willing to provide a complete example that does not require them to do anything, even if you think the amount of work is trivial. – William Pursell Dec 29 '22 at 22:51

1 Answers1

1

The line of code that is causing your error message (shown below) is using what is known as a compound literal. This allows an anonyomus object of the type specified in the parentheses to be constructed from the data provided in the initializer list (inside the braces).

*head = (struct item) { player.sector, 0, W-1 };

The problem you're facing is that this construct was only incorporated into the C language in the "C99" (formally, ISO/IEC 9899:1999) version of the Standard – which, as it's name implies, was published in 1999.

However, the compiler you are using (Turbo C, v3.2) was released in or around 1993, so it almost certainly does not support this feature. (Some pre-1999 compilers may have supported compound literals as extensions but I doubt that Turbo C was one of them.)

There are two things you can do to circumvent the error:

  1. Use a more modern (i.e. less than 20 years old) compiler.
  2. Create an explicit temporary structure and assign that to the *head target:
    struct item temp = { player.sector, 0, W - 1 };
    *head = temp;

Note that, in the second case, your temp variable will have the same scope (and lifetime) as the anonymous object created by the compound literal.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83