0

I'm trying to compile my code from the old MS-DOS days and this doesn't seem to work with GCC:

    typedef struct { int x,y,z; } vector;

    inline void vect_add(vector& c,vector a, vector b)
    {
        c.x=a.x+b.x;
        c.y=a.y+b.y;
        c.z=a.z+b.z;
    }

Basically I'm trying to return a struct which is later used as vector.x etc instead of rewriting it as a pointer to struct and rewriting all as vector->x etc

see (vector& c,

7
  • 3
    C does not have the C++ style reference syntax which you are trying to use. Commented Oct 25, 2021 at 13:59
  • C doesn't have references. You need to emulate it using pointers. Commented Oct 25, 2021 at 14:00
  • As mentioned by @EugeneSh. the syntax you try to use is C++. Are you sure that the project you're trying to build is not a C++ project? Commented Oct 25, 2021 at 14:00
  • I assume this used to work? So you might need to make sure GCC correctly runs in C++ mode, not in C mode. Commented Oct 25, 2021 at 14:00
  • What's the name of the source code file? Are you using g++ or gcc? Commented Oct 25, 2021 at 14:01

1 Answer 1

1

That is possibly valid C++, but it's not valid C.

In C, you need to use a pointer, a global var, or actually return a struct.

typedef struct { int x, y, z; } vector;

inline void vect_add(vector* c, vector a, vector b)
{
    c->x = a.x + b.x;
    c->y = a.y + b.y;
    c->z = a.z + b.z;
}

vect_add(&c, a, b);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.