> >> But "typedef char * string;" is unlikely to be a good plan. At the
> >> very least, use:
> >>
> >> typedef struct { char * s; } string;
And with that you can't pass it str* functions which is
presumably the point of naming it 'string'
> The problem with that is that "string" in C is not a type, so there
> is no existing type that can reasonably be named "string".
A rational number is not simply a pair of integers. Since a
typedef cannot express that, rather than
typedef struct {int num; unsigned den;} Rational;
and letting all the rational* functions imply the rest of type,
you are recommending I instead do
typedef struct {int integer1; unsigned integer2;} IntegerPair;
because the implicit documentation that the integer pair is
intended to represent a rational is outweighed by the typedef not
completely and explicitly forcing it to be rational. Like the
typedef allows (IntegerPair){1, 0}, but you would need
rationalnumber(1, 0) to enforce that restriction.
> Maybe C *should* have had a string type, but it doesn't. Which is
> why I prefer to deal with pointers and arrays as pointers and arrays.
I prefer to deal with abstractions and try to contain
implementation details to a single place.
So rat.h would define the type Rat as
typedef struct {int num; unsigned den} Rat;
Rat ratnumber (int num, int den);
// Return a rational number with a positive den
// and no common factor in num and den. Or
// return r such that ratisnan(r).
bool ratisrat (Rat r);
// Whether r is a valid rational.
bool ratisnan (Rat r);
// Whether r is an invalid rational.
int ratnum (Rat r);
// The numerator of r if ratisrat(r).
int ratden (Rat r);
// The denominator of r if ratisrat(r).
Rat ratadd (Rat q, Rat r);
// Add rationals.
// ratnumber(ratnum(q)*ratden(r)+ratnum(r)*ratden(q),
// ratden(q)*ratden(r))
// if ratisrat(q) and ratisrat(r).
...
Compile rat.c into rat.a and document you use rationals with
cc -lrat source.c
#include "rat.h"
--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
The first law of discordiamism: The more energy This post / \
to make order is nore energy made into entropy. insults Islam. Mohammed