472,805 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

need help parse this difficult type

hi

This code is taken from a lisp interpreter by Andru Luvisi.

typedef struct obj {
enum otype type;
struct obj *p[1]; // array of pointer, sizeof(obj)=8
} obj;
typedef obj * (*primop)(obj *);
obj *all_symbols, *top_env, *nil, *tee, *quote,
*s_if, *s_lambda, *s_define, *s_setb;

Can someone explain what is "typedef obj * (*primop)(obj *);"?
The subsequent line is for a bunch of pointers to "obj" that I
understand.
I understand "typedef" is like alias.
"typedef a b" is like "b is of an alias for a"
The way the code is written, I can't tell what is being aliased.
"(*)" usually means pointer to function that I understand,
My guess is:
// primop is a pointer to a function that takes (obj*) and returns
"obj*"

If that is correct, what is being aliased? Thanks.

Mar 5 '07 #1
6 1692
di*******@yahoo.com wrote:
hi

This code is taken from a lisp interpreter by Andru Luvisi.

typedef struct obj {
enum otype type;
struct obj *p[1]; // array of pointer, sizeof(obj)=8
} obj;
typedef obj * (*primop)(obj *);
My guess is:
// primop is a pointer to a function that takes (obj*) and returns
"obj*"

If that is correct, what is being aliased? Thanks.
Good guess!

"primop someFn" can be used as a shorthand for "obj* (*someFn)(obj*)"
wherever a function with this signature is required, for example in
function parameters or as structure members.

So

obj* foo( obj* (*someFn)(obj*), obj* object ) { return someFn(object);}

can be written

obj* foo( primop someFn, obj* object) { return someFn(object);}

--
Ian Collins.
Mar 5 '07 #2
di*******@yahoo.com wrote On 03/05/07 16:01,:
hi

This code is taken from a lisp interpreter by Andru Luvisi.

typedef struct obj {
enum otype type;
struct obj *p[1]; // array of pointer, sizeof(obj)=8
} obj;
typedef obj * (*primop)(obj *);
obj *all_symbols, *top_env, *nil, *tee, *quote,
*s_if, *s_lambda, *s_define, *s_setb;

Can someone explain what is "typedef obj * (*primop)(obj *);"?
The subsequent line is for a bunch of pointers to "obj" that I
understand.
I understand "typedef" is like alias.
"typedef a b" is like "b is of an alias for a"
The way the code is written, I can't tell what is being aliased.
"(*)" usually means pointer to function that I understand,
My guess is:
// primop is a pointer to a function that takes (obj*) and returns
"obj*"
Correct.
If that is correct, what is being aliased? Thanks.
`primop' is an alias for the type `obj* (*)(obj*)'.
Since `obj' is also an alias, the "full" type is
`struct obj* (*)(struct obj*)'.

--
Er*********@sun.com
Mar 5 '07 #3
On Mar 6, 5:01 am, dillog...@yahoo.com wrote:
hi

This code is taken from a lisp interpreter by Andru Luvisi.

typedef struct obj {
enum otype type;
struct obj *p[1]; // array of pointer, sizeof(obj)=8} obj;

typedef obj * (*primop)(obj *);
obj *all_symbols, *top_env, *nil, *tee, *quote,
*s_if, *s_lambda, *s_define, *s_setb;

Can someone explain what is "typedef obj * (*primop)(obj *);"?
The subsequent line is for a bunch of pointers to "obj" that I
understand.
I understand "typedef" is like alias.
"typedef a b" is like "b is of an alias for a"
The way the code is written, I can't tell what is being aliased.
"(*)" usually means pointer to function that I understand,
My guess is:
// primop is a pointer to a function that takes (obj*) and returns
"obj*"

If that is correct, what is being aliased? Thanks.

One more question: "struct obj *p[1]" "p" is array (1 element) of
pointer to "obj" right? The subsequent usage in the code make me doubt
that, because I see usage like :
#define car(X) ((X)->p[0])
#define cdr(X) ((X)->p[1])
....
#define procenv(X) ((X)->p[2])

wouldn't p[1] seg fault right away? I don't see ways to expand an
array in c language. (malloc is different, we can't assume p[1] is
located immediately after p[0], if p[1] is somehow malloc'ed. I just
can't see how p[1] or p[2] can be used syntax -wise in any fashsion.
Thanks. (the obj creation code follows.)

obj *omake(enum otype type, int count, ...) {
obj *ret;
va_list ap;
int i;
va_start(ap, count);
ret = (obj *) malloc(sizeof(obj) + (count - 1)*sizeof(obj *));
ret->type = type;
for(i = 0; i < count; i++) ret->p[i] = va_arg(ap, obj *);
va_end(ap);
return ret;
}

Mar 5 '07 #4
di*******@yahoo.com wrote On 03/05/07 16:17,:
On Mar 6, 5:01 am, dillog...@yahoo.com wrote:
>>hi

This code is taken from a lisp interpreter by Andru Luvisi.

typedef struct obj {
enum otype type;
struct obj *p[1]; // array of pointer, sizeof(obj)=8} obj;

typedef obj * (*primop)(obj *);
obj *all_symbols, *top_env, *nil, *tee, *quote,
*s_if, *s_lambda, *s_define, *s_setb;

Can someone explain what is "typedef obj * (*primop)(obj *);"?
The subsequent line is for a bunch of pointers to "obj" that I
understand.
I understand "typedef" is like alias.
"typedef a b" is like "b is of an alias for a"
The way the code is written, I can't tell what is being aliased.
"(*)" usually means pointer to function that I understand,
My guess is:
// primop is a pointer to a function that takes (obj*) and returns
"obj*"

If that is correct, what is being aliased? Thanks.

One more question: "struct obj *p[1]" "p" is array (1 element) of
pointer to "obj" right? The subsequent usage in the code make me doubt
that, because I see usage like :
#define car(X) ((X)->p[0])
#define cdr(X) ((X)->p[1])
...
#define procenv(X) ((X)->p[2])

wouldn't p[1] seg fault right away? [...]
This is Question 2.6 in the comp.lang.c Frequently Asked
Questions (FAQ) list at <http://www.c-faq.com/>.

--
Er*********@sun.com
Mar 5 '07 #5
di*******@yahoo.com wrote:
>
One more question: "struct obj *p[1]" "p" is array (1 element) of
pointer to "obj" right? The subsequent usage in the code make me doubt
that, because I see usage like :
#define car(X) ((X)->p[0])
#define cdr(X) ((X)->p[1])
....
#define procenv(X) ((X)->p[2])

wouldn't p[1] seg fault right away? I don't see ways to expand an
array in c language. (malloc is different, we can't assume p[1] is
located immediately after p[0], if p[1] is somehow malloc'ed. I just
can't see how p[1] or p[2] can be used syntax -wise in any fashsion.
Thanks. (the obj creation code follows.)

obj *omake(enum otype type, int count, ...) {
obj *ret;
va_list ap;
int i;
va_start(ap, count);
ret = (obj *) malloc(sizeof(obj) + (count - 1)*sizeof(obj *));
ret->type = type;
for(i = 0; i < count; i++) ret->p[i] = va_arg(ap, obj *);
va_end(ap);
return ret;
}
The form struct "obj *p[1]" is a common kludge where the actual number
of objects varies between object types. As you can see in the create
code, space is reserved for the required number of obj* and they are
initialised form the variable argument list, so they can be safely accessed.

--
Ian Collins.
Mar 5 '07 #6
On Mar 6, 10:01 am, dillog...@yahoo.com wrote:
>
typedef obj * (*primop)(obj *);

I understand "typedef" is like alias.
"typedef a b" is like "b is of an alias for a"
A good way of reading typedefs is to first drop the keyword 'typedef':

obj * (*primo)(obj *);

This declares an object named 'primo'. Now, the effect of the
typedef will be to declare a type name 'primop' that aliases
the type of 'primo'. (This is easier to understand than write!)

Mar 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jari Kujansuu | last post by:
I can successfully parse XML document using SAX or DOM and I can also validate XML document against schema. Problem is that my program should deal with user-defined schemas which means that when...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
3
by: Bill Borg | last post by:
Hello all, Not sure what to call it, but when you go to some sites, you are forced to retype a graphic (looks like graffiti) password to continue. I presume this is to thwart bots, DOS attacks,...
2
by: Redstone | last post by:
For a Lodging reservatgions system, I need to build in a Flowchart type Calendar. Also called a "Tape" in the lodging business. This has a list of rental units down the left side and dates along...
10
by: Tomás | last post by:
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs...
1
by: Sharon | last post by:
I need to write an XML document, that other users can work with to change values and to add elements. My problem is that for each element that me or any other user will add, should have some...
1
by: ellischristina | last post by:
Hello. I am trying to find a legal type database that includes the ability to have multiple party information sections or pages. i.e. a petitioner and a respondant. Hopefully it would allow...
2
!NoItAll
by: !NoItAll | last post by:
I have a ListOf(Type that I've created that will often have well over 20-thousand entries. The type I am placing into the list looks like this: Structure MyType Dim FileName as String ...
1
by: David Yarbrough | last post by:
Call statements reads : Call AuditTrail (Me, RqmntsID) where RqmntsID is the tables primary key and an autonumber field AuditTrail is defined as: Sub AuditTrail(frm As Form, recordid As Control)
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.