How to find out if an int has no value

G

Gregory Pietsch

Richard said:
Okay, than I should be more spesific.
I've got this:

typdef struct
{
int x;
int y;
} Point;

// I now want to make a function that checks if a Point is empty

int pointEmpty(Point p)
{
return (p /*isEmpty*/ ? 1 : 0)
}

How should I do that?
TIA


One way: add a value to the structure:

typedef struct {
int x, y, is_empty;
} Point;

then the implementation of the function is easy:

int pointEmpty(Point p)
{
return (p.is_empty != 0);
}

Just remember to initialize the value somehow.

Gregory Pietsch
 
R

Richard

Richard said:
berlin.de:
[..]
How can a point be empty? As said by Joona scalar Types always have
values, but you're responsible that they get initialized correctly.

Or do you want to check if the point is (0,0)?
I want to make a fail save for myself. So, if I'd wrote

Point p;

somewhere without initializing it, I could check it whit my

Ok.
IMHO it is good style to write "constructorfunctions", and geting
variables only over this functions. For example:

Point newPoint(int x, int y);

or better:
Point *newPoint(int x, int y);

So you can never get problems with uninitialized values, because
you're simulating a constructor.

With this way also unnecessary extraflags can be avoided.

Kind regards,
Nicolas

I like 'simulating a constructor'. I didn't know that it was good c
practice to simulate oo-programming. Thanks!

Kind regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top