473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1719
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.