Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   typedef struct (http://www.velocityreviews.com/forums/t437891-typedef-struct.html)

Russell Shaw 04-30-2005 03:35 PM

typedef struct
 
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] ?

Ben Pfaff 04-30-2005 04:03 PM

Re: typedef struct
 
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:

> 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.
--
Bite me! said C.

Mike Wahler 04-30-2005 04:12 PM

Re: typedef struct
 
"Russell Shaw" <rjshawN_o@s_pam.netspace.net.au> wrote in message
news:46lck2-305.ln1@main.anatron.com.au...
> 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



Rob Morris 05-03-2005 12:18 PM

Re: typedef struct
 
Russell Shaw wrote:
> 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.

--
Rob Morris: arr emm four four five [at] cam dot ac dot uk

Russell Shaw 05-03-2005 04:14 PM

Re: typedef struct
 
Rob Morris wrote:
> Russell Shaw wrote:
>
>> 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.

Keith Thompson 05-03-2005 08:49 PM

Re: typedef struct
 
Rob Morris <rm445@cam.spam.ac.uk> writes:
> Russell Shaw wrote:
>> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


All times are GMT. The time now is 09:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.