typedef struct

Discussion in 'C Programming' started by Russell Shaw, Apr 30, 2005.

  1. Russell Shaw

    Russell Shaw Guest

    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] ?
    Russell Shaw, Apr 30, 2005
    #1
    1. Advertising

  2. Russell Shaw

    Ben Pfaff Guest

    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.
    Ben Pfaff, Apr 30, 2005
    #2
    1. Advertising

  3. Russell Shaw

    Mike Wahler Guest

    "Russell Shaw" <rjshawN_o@s_pam.netspace.net.au> wrote in message
    news:...
    > 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
    Mike Wahler, Apr 30, 2005
    #3
  4. Russell Shaw

    Rob Morris Guest

    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
    Rob Morris, May 3, 2005
    #4
  5. Russell Shaw

    Russell Shaw Guest

    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.
    Russell Shaw, May 3, 2005
    #5
  6. Rob Morris <> 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) <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.
    Keith Thompson, May 3, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    494
  2. DanielEKFA
    Replies:
    8
    Views:
    593
    DanielEKFA
    May 16, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,358
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    664
  5. oor
    Replies:
    0
    Views:
    1,336
Loading...

Share This Page