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

Recommended Answers

All 4 Replies

define a structure

struct coord
{
   int x;
   int y;
   int z;
};
commented: Yep so right +0

Ok I have another question. I want to make sure this is a good idea. 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

Exactly right. Structs are used in C, to combine related variables, into one object.

you don't want to use a union in your program because you need the value of all the object at the same time. unions won't allow that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.