Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gontjarow opened this issue Nov 13, 2020 · 2 comments
Open

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

Gontjarow opened this issue Nov 13, 2020 · 2 comments

Comments

@Gontjarow
Copy link

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 Gontjarow changed the title Inline struct-declaration doesn't work. Assignment of struct compound-literal doesn't work. Nov 13, 2020
@Gontjarow
Copy link
Author

Gontjarow commented Nov 13, 2020

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
Copy link

brdjns commented Nov 11, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants