473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Typedefing structs as arrays of size 1

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!

Sep 27 '07 #1
4 1241
Fred <no****@nospam.comwrote:
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
Sep 27 '07 #2
Richard Bos wrote:
Fred <no****@nospam.comwrote:
>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 "&"...
Sep 27 '07 #3
In article <sl*******************@nospam.com>, Fred <no****@nospam.comwrote:
[...]

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.
Sep 27 '07 #4
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:Th*************@gmail.com>

Sep 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
7
by: Rennie deGraaf | last post by:
A question regarding this code, which defines a struct containing a size and a variable-sized array: typedef struct { uint16_t count; unsigned char bytes; } foo_t; ....
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
7
by: aemado | last post by:
I am trying to write a program that reads in data from a user-specified file. I have to sort this file two ways, one using parallel arrays and the other using structs. I then have to output the...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.