473,320 Members | 1,865 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,320 software developers and data experts.

TypeDef array of size 1

CJ
Hello friends,

It seems to be quite common in libraries (e.g. GMP) to have typedefs
like

typedef struct {
/* stuff */
} __type_struct;
typedef __type_struct type_t[1];

I guess the advantage of this is that it allows type_t variables to be
passed by reference without an extra level of indirection.

Is this considered good C style? Are there any problems or pitfalls to
be wary of with this approach?

Nov 30 '07 #1
1 2601
CJ wrote:
Hello friends,

It seems to be quite common in libraries (e.g. GMP) to have typedefs
like

typedef struct {
/* stuff */
} __type_struct;
typedef __type_struct type_t[1];

I guess the advantage of this is that it allows type_t variables to be
passed by reference without an extra level of indirection.
That can also be archived with

typedef __type_struct* ptr_type_t;

The point with "typedef T type_t[1]", is allocating an object at
declaration, and yes, simultaneously hide the pointer issue in function
calls.
Is this considered good C style?
Not in my book... but ... that doesn't count for much.
Are there any problems or pitfalls to be wary of with this approach?
Hmm.. a pitfall for me, would be forgetting e.g. a mpz_t object, being a
pointer to a struct. Hiding such information, feel like obfuscation, but
others will prefer

typedef __mpz_struct mpz_t[1];
mpz_t a;

mpz_init(a);

over my preference, which is rather an API design like this

typedef __mpz_struct mpz_t;
mpz_t a;

mpz_init(&a);
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Nov 30 '07 #2

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

Similar topics

5
by: lou zion | last post by:
hi all, i want to create a vector where each element is an array of 40 doubles. is this valid? something like: void abc( std::vector<double> InVals) { typedef double DataSeriesType;...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
5
by: Russell Shaw | last post by:
Hi, In setjmp.h on a linux system, there is: typedef struct __jmp_buf_tag { ... } jmp_buf; I could understand typedef struct { } jmp_buf, but how do i interpret typedef struct { } jmp_buf ?
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
7
by: Francois Grieu | last post by:
Hello, is the size of a type, defined using typedef as a collection of arrays of unsigned char, the sum of the size of the arrays ? After simplifying my question, it probably is equivalent to:...
7
by: mshaaban | last post by:
Hello Sir, In my code I have made a typedef 2D array (large array) typedef char matrix; Then in the program I used malloc to allocate memory for the array matrix *m; m =malloc(sizeof *m);...
4
by: AalaarDB | last post by:
Basically, how do I do this? If there is no legal way to do what I want, why not? It seems very logical. I want the array as a typedef for elegance and so I can do treasury.front() or treasury ...
3
by: codenamezero | last post by:
I am using C, there is no dynamic array. So i tried this and it worked... but want to know if this can be platform independent so i can actually use this across any platform. typedef struct {...
2
by: Alex Snast | last post by:
Hello guys, I can't seem to be able to get accsess to a type i declared within a template class i.e. template <typename T> class Array { public: typedef std::size_t size_type;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.