drh / lcc

The lcc retargetable ANSI C compiler
https://drh.github.io/lcc/
2.03k stars 442 forks source link

Assignment of struct compound-literal doesn't work. #43

Open Gontjarow opened 4 years ago

Gontjarow commented 4 years ago

The below program will output 0.000000 for all 6 values.

#include <stdio.h>

typedef struct xy
{
    double x, y;
} t_xy;

t_xy vec(double x, double y)
{
    return ( (t_xy){x, y} );
}

t_xy operator+ (t_xy &a, t_xy &b)
{
    return ( (t_xy){a.x + b.x, a.y + b.y} );
}

int main(int argc, char *argv[])
{
    t_xy a = vec(1, 2);
    t_xy b = vec(2, 4);
    t_xy c = a + b;
    printf("a == %f %f\n", a.x, a.y);
    printf("b == %f %f\n", b.x, b.y);
    printf("c == %f %f\n", c.x, c.y);
}

The same applies even to the simplest case:

t_xy test = (t_xy){1.0, 2.0};
printf("test == %f %f\n", test.x, test.y);
Gontjarow commented 4 years ago

Additionally, the following will not work (all zeroes on out):

t_xyz overloaded vec(double x, double y, double z)
{
    printf("calling vec3 (in:  %f %f %f)\n", x, y, z);
    t_xyz out = {x, y, z};
    printf("calling vec3 (out: %f %f %f)\n\n", out.x, out.y, out.z);
    return (out);
}

But the following will work:

t_xyz overloaded vec(double x, double y, double z)
{
    printf("calling vec3 (in:  %f %f %f)\n", x, y, z);
    t_xyz out;
    out.x = x;
    out.y = y;
    out.z = z;
    printf("calling vec3 (out: %f %f %f)\n\n", out.x, out.y, out.z);
    return (out);
}
brdjns commented 2 years ago

LCC is a C89 compiler. It doesn't support compound literals, which were introduced with C99.