Typedefing structs as arrays of size 1

Discussion in 'C Programming' started by Fred, Sep 27, 2007.

  1. Fred

    Fred Guest

    Can anyone explain what the point of this is?

    For example, code like:

    typedef struct {
    int alloc;
    char *data;
    } my_struct;

    typedef my_struct my_type_t[1];

    What's the advantage of making the my_type_t type an array of 1
    my_struct? Why not either just make it a plain typedef for the struct,
    or else make it a typedef for a pointer to the struct?

    TIA!
     
    Fred, Sep 27, 2007
    #1
    1. Advertising

  2. Fred

    Richard Bos Guest

    Fred <> wrote:

    > Can anyone explain what the point of this is?
    >
    > For example, code like:
    >
    > typedef struct {
    > int alloc;
    > char *data;
    > } my_struct;
    >
    > typedef my_struct my_type_t[1];
    >
    > What's the advantage of making the my_type_t type an array of 1
    > my_struct? Why not either just make it a plain typedef for the struct,
    > or else make it a typedef for a pointer to the struct?


    Without context, there seems to be no point. How is my_type_t used?

    Richard
     
    Richard Bos, Sep 27, 2007
    #2
    1. Advertising

  3. Fred

    Mark Bluemel Guest

    Richard Bos wrote:
    > Fred <> wrote:
    >
    >> Can anyone explain what the point of this is?
    >>
    >> For example, code like:
    >>
    >> typedef struct {
    >> int alloc;
    >> char *data;
    >> } my_struct;
    >>
    >> typedef my_struct my_type_t[1];
    >>
    >> What's the advantage of making the my_type_t type an array of 1
    >> my_struct? Why not either just make it a plain typedef for the struct,
    >> or else make it a typedef for a pointer to the struct?

    >
    > Without context, there seems to be no point. How is my_type_t used?


    There was a discussion of this sort of thing very recently. The normal
    reason seems to be to allow a my_struct to be, in effect, passed by
    reference without using "&"...
     
    Mark Bluemel, Sep 27, 2007
    #3
  4. In article <>, Fred <> wrote:
    [...]

    Another post through an anonymous server, asking a question that was
    asked just a week or two ago. I don't know whether it's a troll,
    or someone on a course whose tutor wants us to do the teaching, or what,
    but I suggest not answering questions with these characteristics.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 27, 2007
    #4
  5. Fred wrote:
    >
    > Can anyone explain what the point of this is?
    >
    > For example, code like:
    >
    > typedef struct {
    > int alloc;
    > char *data;
    > } my_struct;
    >
    > typedef my_struct my_type_t[1];
    >
    > What's the advantage of making the my_type_t type an array of 1
    > my_struct? Why not either just make it a plain typedef for the struct,
    > or else make it a typedef for a pointer to the struct?


    It's an attempt to do things like "pass by reference" rather than
    "pass by value" when referring to my_type_t.

    (Crossing fingers and hoping I don't mess this up.)

    Consider:

    void func(void)
    {
    my_struct foo;
    my_type_t bar;

    func2(foo,bar);
    }

    Here, "foo" will be passed by value, but bar will be passed by
    reference (sort of) because it's an array.

    It has an advantage over a pointer-to-struct, because:

    typedef my_struct *my_type_t2;
    ...
    my_type_t2 foobar;

    will not allocate storage for the struct.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 27, 2007
    #5
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    660
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    578
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    654
    Eric Sosman
    Oct 11, 2005
  4. aalaardb

    Typedefing an array

    aalaardb, Jun 9, 2007, in forum: C Programming
    Replies:
    1
    Views:
    345
    aalaardb
    Jun 14, 2007
  5. David Marsh

    typedefing a struct

    David Marsh, Sep 14, 2007, in forum: C Programming
    Replies:
    15
    Views:
    582
Loading...

Share This Page