Typedefing structs as arrays of size 1

F

Fred

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!
 
R

Richard Bos

Fred said:
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
 
M

Mark Bluemel

Richard said:
Fred said:
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 "&"...
 
R

Richard Tobin

[...]

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
 
K

Kenneth Brody

Fred said:
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:[email protected]>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top