typedef struct

R

Russell Shaw

Hi,
In setjmp.h on a linux system, there is:

typedef struct __jmp_buf_tag
{
...
} jmp_buf[1];

I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?
 
B

Ben Pfaff

Russell Shaw said:
I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?

It declares a jmp_buf as an array of 1 element of the given
structure. The C standard says that jmp_buf is an array type, so
this declaration fulfills that requirement.
 
M

Mike Wahler

Russell Shaw said:
Hi,
In setjmp.h on a linux system, there is:

typedef struct __jmp_buf_tag
{
...
} jmp_buf[1];

I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?

The type of the name 'jmp_buf' is
"array of one struct __jmp_buf_tag".

I.e.

jmp_buf j;
/* is equivalent to: */
struct __jmp_buf_tag j[1];


-Mike
 
R

Rob Morris

Russell said:
Hi,
In setjmp.h on a linux system, there is:

typedef struct __jmp_buf_tag
{
...
} jmp_buf[1];

I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?

Others have pointed out that the above means jmp_buf is an alias for an
array size 1 of struct __jmp_buf_tag.

I tend to lurk rather than post here, but it occurs to me that it might
be helpful to you to know that the above is the normal syntax for using
typedef to give you a name for an array.

For example, if you wanted to store 3D coordinates, you might use a type
called 'vector' that's an array of 3 doubles (not to be confused with a
computer-sciencey vector which is something else). So you'd write:

typedef double vector[3];
/* looks a bit counterintuitive I guess */

vector position = {0.0, 1.2, 3.4}, velocity={0.0, 1.0, 0.0};
position[0] = 1.5; /* etc, just use like any array */

Hope this helps.
 
R

Russell Shaw

Rob said:
Russell said:
Hi,
In setjmp.h on a linux system, there is:

typedef struct __jmp_buf_tag
{
...
} jmp_buf[1];

I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?


Others have pointed out that the above means jmp_buf is an alias for an
array size 1 of struct __jmp_buf_tag.

I tend to lurk rather than post here, but it occurs to me that it might
be helpful to you to know that the above is the normal syntax for using
typedef to give you a name for an array.

For example, if you wanted to store 3D coordinates, you might use a type
called 'vector' that's an array of 3 doubles (not to be confused with a
computer-sciencey vector which is something else). So you'd write:

typedef double vector[3];
/* looks a bit counterintuitive I guess */

vector position = {0.0, 1.2, 3.4}, velocity={0.0, 1.0, 0.0};
position[0] = 1.5; /* etc, just use like any array */

Hope this helps.

Yes, i somehow got in to a mode of confusion. I already knew how
to interpret things like: typedef int (afunc*)(void *, int), so
typedefs of arrays are really just as easy.
 
K

Keith Thompson

Rob Morris said:
Russell said:
Hi,
In setjmp.h on a linux system, there is:
typedef struct __jmp_buf_tag
{
...
} jmp_buf[1];
I could understand typedef struct { } jmp_buf, but how do i
interpret typedef struct { } jmp_buf[1] ?

Others have pointed out that the above means jmp_buf is an alias for
an array size 1 of struct __jmp_buf_tag.

I tend to lurk rather than post here, but it occurs to me that it
might be helpful to you to know that the above is the normal syntax
for using typedef to give you a name for an array.

For example, if you wanted to store 3D coordinates, you might use a
type called 'vector' that's an array of 3 doubles (not to be confused
with a computer-sciencey vector which is something else). So you'd
write:

typedef double vector[3];
/* looks a bit counterintuitive I guess */

vector position = {0.0, 1.2, 3.4}, velocity={0.0, 1.0, 0.0};
position[0] = 1.5; /* etc, just use like any array */

Hope this helps.

Yes, that's legal, but it's likely to be a bad idea. For example,
this would be illegal:

vector v0 = {0.0, 1.2, 3.4};
vector v1 = v0; /* Illegal, can't assign arrays */
vector v2 = vector_sum(v0, v1);
/* Illegal, functions can't return arrays */

For this particular application, it would make more sense to use:

typedef struct {
double x;
double y;
double z;
} vector;

which quietly makes the code above legal (except that "position[0]"
becomes "position.x", which is probably more legible anyway).

Since arrays aren't first-class objects in C, hiding the array-ness of
a type behind a typedef is usually a bad idea. (Hiding the
struct-ness of a type behind a typedef is arguably a bad idea as
well.) jmp_buf is a rare exception, but only because the standard
specifically requires it to be an array type.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top