Return an array in EasyC

I’m writing a program to add vectors in EasyC. However, this program needs to return two double values. Is there a way to return an array, or return multiple values in EasyC?

I don’t know how easyC works but int] functionName(){ code… Return array}
Might work.

I don’t think pointers are supported in Easy C. I am no expert on Easy C though.

Pointers are supported.

EasyC is gcc.

Since pointers are on the table what you want is a pass by reference…

// Set's foo to 4, and bar to 2
void doWhatever(int * foo, int * bar)
{
	*foo = 4;
	*bar = 2;
}

So for vector adding (let’s assume 2D vectors for simplicity)…

void add2DVector(int Ax, int Ay, int Bx, int By, int * Cx, int * Cy)
{
	*Cx = Ax + Bx;
	*Cy = Ay + By;
}

Calling this would look like this…

int x, y;

void add2DVector(5, 5, 5, 5, &x, &y);

The values you want will overwrite x and y once the function has run.

That’s neat, but it’s a hassle, so I would suggest something different, why don’t we make some structs?

// 2D struct
typedef struct{
	int x;
	int y;
} Vec2D;

// 3D struct
typedef struct{
	int x;
	int y;
	int z;
} Vec3D;

Vec2D add2DVector(Vec2D a, Vec2D b)
{
	Vec2D r;
	r.x = a.x + b.x;
	r.y = a.y + b.y;
	return r;
}

Vec2D add3DVector(Vec3D a, Vec3D b)
{
	Vec3D r;
	r.x = a.x + b.x;
	r.y = a.y + b.y;
	r.z = a.z + b.z;
	return r;
}


// main.c
Vec2D foo, bar, baz;
foo.x = 5;
foo.y = 5;
bar.x = 5;
bar.y = 5;

baz = add2DVector(foo, bar);

I don’t know what that means.

I created two global variable for the outputs of the vectors. If there’s another way, however, I’d be glad to change my code.

Sorry, I didn’t see Cody’s reply before I replied.

I don’t know if I can create classes in EasyC, but I will try. Thanks!

It means that “under the hood” EasyC uses the GNU C Compiler.
GNU Compiler Collection

Cody pretty much gave you the answer but really didn’t explain how to enter that in EasyC. Search on EasyC user functions, that’s the easiest way to implement this.

I wrote it with the “User Code” blocks. I’ll try it out when I get to a cortex.

James is telling you EasyC is GCC so that you understand that it supports pretty much all the things you can do in C, including pointers.

Cody doesn’t know how to enter that in EasyC. :stuck_out_tongue:

That’s one way, this may be easier.
EasyC - writing user functions in C

but perhaps that’s how you did it.

Yes, that is how I did it.

GCC rocks! The Gnu tools are old school but are the foundational elements of what great coders use. Gnu compiler collection or gcc (released in 1987) married with other tools like grep (1974) make (1977), emacs (1976), grprof (1988), gzip, strings, and many more make up so much of what we call Unix distributions.

Get to know your gnu tools. They help you master unix operating systems and programming. It will do you well. “It’s not unix” but Gnu tools are really what makes Unix & Linux in particular so great. They ported these tools to many O/S variants but Unix O/S’s are the ones that stand out. We would not have Linux without these sets of tools.

If you are on a Mac, look under the hood… What’s under there? :eek:

http://www.gnu.org/software/software.html

Saw this when I did the image search for gcc and had to put it in…
http://shop.fsf.org/static/images/productimage-picture-run-gcc-shirt-135

It worked! Thank you very much.