473,513 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one element array as name of struct typedef

I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?

Sep 4 '07 #1
17 3601
Ron Peterson (012ED25E) wrote:
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?

Hmmm.... I think you're mistaken in what this means.

My reading (which could easily be incorrect) is that it makes "foo" a
typedef for a single dimension, single element, array of such structures.

I'm trying to work out what the value of such a technique would be.
Sep 4 '07 #2
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>typedef struct {
int len;
char *buf;
} foo[1];
>I'm trying to work out what the value of such a technique would be.
My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;

you would have to use &foo all over the place, and it it had been

typedef struct {...} *foo;

you would have to allocate the data as well as declaring a variable.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 4 '07 #3
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?
typeof(foo) == array of the given struct of 1 element

so the actual array may be declared or defined as:

foo fooarray; /* Note: No dimension info required */

the technique is useful when a pointer to an array (i mean entire array, not
just pointer to element data type) is required.
read C FAQ. Ther is a entire chapter devoted to pointers and array.


Sep 4 '07 #4

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:fb***********@pc-news.cogsci.ed.ac.uk...
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
typedef struct {
int len;
char *buf;
} foo[1];
I'm trying to work out what the value of such a technique would be.

My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;
No. The typedef is similar to , say..

typedef unsigned int length_t;

Do we use &length_t anywhere ? the length_t is the *name* of a *type*.

similarly with foo. could have been named better as "foo_array_type"


Sep 4 '07 #5
On Tue, 04 Sep 2007 12:17:34 +0100, Mark Bluemel wrote:
Ron Peterson (012ED25E) wrote:
>I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?


Hmmm.... I think you're mistaken in what this means.

My reading (which could easily be incorrect) is that it makes "foo" a
typedef for a single dimension, single element, array of such structures.

I'm trying to work out what the value of such a technique would be.
On glibc, jmp_buf is declared as an array with one element.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Sep 4 '07 #6
In article <fb**********@news4.fe.internet.bosch.com>,
Ravishankar S <s.***********@de.bosch.comwrote:
>My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;
>No. The typedef is similar to , say..
Sorry, I just made a mistake in the line you snipped. What I should
have said was:

If it had been

typedef struct {...} mpz_t;

than after declaring

mpz_t x;

you would have to use &x all over the place.

Declaring the type as a single-element array is a way of hiding from
the user the fact that it's a structure and needs to have its address
passed to the library functions.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 4 '07 #7
On Sep 4, 7:50 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <fbjese$sr...@aioe.org>,
Mark Bluemel <mark_blue...@pobox.comwrote:
typedef struct {
int len;
char *buf;
} foo[1];
I'm trying to work out what the value of such a technique would be.

My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;

you would have to use &foo all over the place, and it it had been

typedef struct {...} *foo;

you would have to allocate the data as well as declaring a variable.
Yes. I was trying to emulate the way the MP library worked, and that
is how I discovered this. It lets you avoid ampersands, and
automatically allocates the structure. So you can do 'foo avar;', and
then pass avar to an initialization function, say, that would take
care of setting the values of 'len' and 'buf'. I rather like the
technique, I'm mostly just wondering if it's standard and portable.

Sep 4 '07 #8
Richard Tobin wrote:
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>>typedef struct {
int len;
char *buf;
} foo[1];
>I'm trying to work out what the value of such a technique would be.

My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;

you would have to use &foo all over the place, and it it had been

typedef struct {...} *foo;

you would have to allocate the data as well as declaring a variable.
And how is this worse than having to artificially use a "[0]" subscript
all the time?

Oh! Is it the case that the code which declares items of type "foo"
never actually work with their contents - effectively treating them as
opaque? Clever, I guess...
Sep 4 '07 #9
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>And how is this worse than having to artificially use a "[0]" subscript
all the time?
You don't...
>Oh! Is it the case that the code which declares items of type "foo"
never actually work with their contents - effectively treating them as
opaque? Clever, I guess...
Exactly. This is code provided by a library.

A more common approach would be to declare the type as a pointer, and
require you to call a function to allocate the object. But this way
you can just declare it in the usual way. Unfortunately it looks as
if you would still have to do something explicit to free it, since it
contains a pointer to a buffer.

(I haven't used the library, I'm just inferring this from the definition.)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 4 '07 #10
Ron Peterson (012ED25E) wrote:
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?
Yes, the semantics of this declaration are fully defined by the C
standard, and the technique is therefor portable.
I am not familiar with using it, so I can't tell if it has a name or
not.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Sep 4 '07 #11
On Sep 4, 12:19 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <fbjnhg$mm...@aioe.org>,
Mark Bluemel <mark_blue...@pobox.comwrote:
And how is this worse than having to artificially use a "[0]" subscript
all the time?

You don't...
Oh! Is it the case that the code which declares items of type "foo"
never actually work with their contents - effectively treating them as
opaque? Clever, I guess...

Exactly. This is code provided by a library.

A more common approach would be to declare the type as a pointer, and
require you to call a function to allocate the object. But this way
you can just declare it in the usual way. Unfortunately it looks as
if you would still have to do something explicit to free it, since it
contains a pointer to a buffer.

(I haven't used the library, I'm just inferring this from the definition.)
Yeah. GNU MP has you do:

mpz_t avar;
mpz_init( avar );
....do something with avar
mpz_clear( avar );

I think this seems cleaner than

struct astruct *avar = NULL;
avar = malloc( sizeof( struct astruct ) );
init_function( &avar );
...do something
clear_function( &avar );
free( avar );

....which, AFAIKT, is the least amount of code you'd have to write to
do the same thing without the clever one element array.
Sep 4 '07 #12
On Sep 4, 2:57 pm, "Ron Peterson (012ED25E)" <peterson....@gmail.com>
wrote:
On Sep 4, 12:19 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <fbjnhg$mm...@aioe.org>,
Mark Bluemel <mark_blue...@pobox.comwrote:
>And how is this worse than having to artificially use a "[0]" subscript
>all the time?
You don't...
>Oh! Is it the case that the code which declares items of type "foo"
>never actually work with their contents - effectively treating them as
>opaque? Clever, I guess...
Exactly. This is code provided by a library.
A more common approach would be to declare the type as a pointer, and
require you to call a function to allocate the object. But this way
you can just declare it in the usual way. Unfortunately it looks as
if you would still have to do something explicit to free it, since it
contains a pointer to a buffer.
(I haven't used the library, I'm just inferring this from the definition.)

Yeah. GNU MP has you do:

mpz_t avar;
mpz_init( avar );
...do something with avar
mpz_clear( avar );

I think this seems cleaner than

struct astruct *avar = NULL;
avar = malloc( sizeof( struct astruct ) );
init_function( &avar );
..do something
clear_function( &avar );
free( avar );

...which, AFAIKT, is the least amount of code you'd have to write to
do the same thing without the clever one element array.
whoops, forget the ampersands of course...

Sep 4 '07 #13
On Sep 5, 6:57 am, "Ron Peterson (012ED25E)" <peterson....@gmail.com>
wrote:
Yeah. GNU MP has you do:

mpz_t avar;
mpz_init( avar );
...do something with avar
mpz_clear( avar );

I think this seems cleaner than

struct astruct *avar = NULL;
avar = malloc( sizeof( struct astruct ) );
init_function( &avar );
..do something
clear_function( &avar );
free( avar );

...which, AFAIKT, is the least amount of code you'd have to write to
do the same thing without the clever one element array.
You overlooked:
avar_t avar;
init_func(&avar);
......
clear_func(&avar);

which is no more complicated than the mpz variation. I
find the technique distasteful, as its only function
appears to be to obfuscate the code. Code which looks
like it passes objects by value, actually modifies the
objects.

Sep 4 '07 #14
Army1987 <ar******@NOSPAM.itwrites:
On Tue, 04 Sep 2007 12:17:34 +0100, Mark Bluemel wrote:
>Ron Peterson (012ED25E) wrote:
>>I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I'm unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?

Hmmm.... I think you're mistaken in what this means.

My reading (which could easily be incorrect) is that it makes "foo" a
typedef for a single dimension, single element, array of such structures.

I'm trying to work out what the value of such a technique would be.
On glibc, jmp_buf is declared as an array with one element.
The standard specifically requires jmp_buf to be an array type.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 5 '07 #15
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>On glibc, jmp_buf is declared as an array with one element.
>The standard specifically requires jmp_buf to be an array type.
Because historically it was just an array of a few ints, into which
some registers were copied.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 5 '07 #16
On Tue, 04 Sep 2007 14:45:20 +0100, Mark Bluemel wrote:
Richard Tobin wrote:
>In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>>>typedef struct {
int len;
char *buf;
} foo[1];
>>I'm trying to work out what the value of such a technique would be.

My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;

you would have to use &foo all over the place, and it it had been

typedef struct {...} *foo;

you would have to allocate the data as well as declaring a variable.

And how is this worse than having to artificially use a "[0]" subscript
all the time?
Well you can use foo->len etc. which I've done before, although I don't
hide it in typedef's.

--
James Antill -- ja***@and.org
C String APIs use too much memory? ustr: length, ref count, size and
read-only/fixed. Ave. 44% overhead over strdup(), for 0-20B strings
http://www.and.org/ustr/
Sep 5 '07 #17
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>>typedef struct {
int len;
char *buf;
} foo[1];
>>I'm trying to work out what the value of such a technique would be.

My guess is that it's a neat trick for having objects that are (in
effect) automatically passed by reference. [...]
For what it's worth, the canonical example of this technique is
the jmp_buf type declared in <setjmp.h>.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Sep 5 '07 #18

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

Similar topics

6
2355
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in...
1
6392
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
5
3717
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz...
10
3365
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
12
3856
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
3755
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
8
3192
by: nobrow | last post by:
Okay ... Im out of practice. Is it not possible to have a 2D array where each column is of a different type, say an int and a struct*? The following seg faults for me and I cant figure out what I...
44
5733
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
16263
by: sahil | last post by:
Hello frends i am learning c language, I want to make a program which count occurence of each element in an array .I write following code for it but ity is not giving me desired result.pls help me....
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
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
7535
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...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.