Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Typedefing structs as arrays of size 1

Reply
Thread Tools

Typedefing structs as arrays of size 1

 
 
Fred
Guest
Posts: n/a
 
      09-27-2007
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!

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      09-27-2007
Fred <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      09-27-2007
Richard Bos wrote:
> Fred <(E-Mail Removed)> 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 "&"...
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-27-2007
In article <(E-Mail Removed)>, Fred <(E-Mail Removed)> 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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      09-27-2007
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: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typedefing a struct David Marsh C Programming 15 09-14-2007 10:57 PM
Typedefing an array aalaardb C++ 1 06-14-2007 10:19 PM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments