tags:

views:

72

answers:

2

I have never programmed in C and have not programmed in C++ for a little while and either way, I am still learning.

I am writing a program in C and am converting a MATLAB program to this C program. I have a lot of variables from MATLAB that are cartesian coordinates, X Y Z, of points. Is there a variable type I could use built into C? I need to subtract different points and also do other operations on them. Any suggestions?

Thanks, DemiSheep


Edit:

Ok I have another question. I want to make sure this is a good idea. (But first, with stackoverflow is this how I should post more information to my post, by editing my original post? -Thanks!)

Question:

I have an object with several characteristics. Say it's a car and the car has several different parameters.

Parameters:

Position on map Altitude (sea level?) Start position End Position

Can I use a struct or union (or both?) to have all these parameters inside the car "object" and update the parameters in the object like you would if it was it's own class in like C++ or Java?

Like if I want to calculate the distance the car traveled from the start position to its current position in like a Cartesian plane I could do something like:

distantTraveled = car.position - car.startPosition

Thanks, DemiSheep

+6  A: 

There are no built-in types that will do what you want.

You'll probably have to make your own small struct type such as the following.

typedef struct {
  double x, y, z;
} coordinate;

If you're limited to C (rather than C++), you'll then need to build up a small library of functions to operate on these data types.

For what it's worth, I highly recommend looking around to find a library that will provide this type of thing for you. Ideally something specialized to the domain you're working in.

jkerian
+1 : I esspecially like the suggestion to find a pre-existing library.
torak
+1  A: 

You would use a struct (typedef struct {double x,y,z;} point;) or an array (double p[3]).

If you want to get more clever you could use both by employing a union:

typedef union {
  struct {double x,y,z;} s;
  double a[3];
} point;

but this comes at the cost of wordier access syntax:

point p;
p.a[2] = 7;
p.s.x = 5;
dmckee
I edited my post please see above. :)
DemiSheep