R
Rouben Rostamian
I am reading the last chapter in Kernighan and Pike's
"The Unix Programming Environment" where they describe their
program "hoc" which, in effect, is a multifunction programmable
calculator.
In the heart of hoc is an array, named prog[], that hold the
calculator's instructions. It is defined as follows (I have taken
the liberty to strip down to essentials):
typedef struct Symbol {
double val;
struct Symbol *fp;
} Symbol;
typedef void (*Inst)(void);
Inst prog[100];
void add(void)
{
... details snipped, but note that `add' is off type Inst ...
}
Then:
int main(void)
{
Symbol *s;
Inst *pc = prog;
s = ... allocate memory and define s ...;
*pc++ = add;
*pc++ = (Inst)s;
...
}
My question is regarding the (Inst) cast in the last line shown in main().
To what extent is it legal to cast s, which a pointer to Symbol,
to type Inst, which is a pointer to a function. Is it safe to assume
than any pointer type can be cast into any other pointer type?
"The Unix Programming Environment" where they describe their
program "hoc" which, in effect, is a multifunction programmable
calculator.
In the heart of hoc is an array, named prog[], that hold the
calculator's instructions. It is defined as follows (I have taken
the liberty to strip down to essentials):
typedef struct Symbol {
double val;
struct Symbol *fp;
} Symbol;
typedef void (*Inst)(void);
Inst prog[100];
void add(void)
{
... details snipped, but note that `add' is off type Inst ...
}
Then:
int main(void)
{
Symbol *s;
Inst *pc = prog;
s = ... allocate memory and define s ...;
*pc++ = add;
*pc++ = (Inst)s;
...
}
My question is regarding the (Inst) cast in the last line shown in main().
To what extent is it legal to cast s, which a pointer to Symbol,
to type Inst, which is a pointer to a function. Is it safe to assume
than any pointer type can be cast into any other pointer type?