All of lore.kernel.org
 help / color / mirror / Atom feed
From: gumbold <gumbold@bonbon.net>
To: linux-c-programming@vger.kernel.org
Subject: Re: data structure question
Date: Mon, 16 May 2005 07:00:30 +0300	[thread overview]
Message-ID: <42881ADE.2010009@bonbon.net> (raw)
In-Reply-To: <17031.23394.834533.766062@gargle.gargle.HOWL>

Glynn Clements wrote:

>gumbold wrote:
>
>  
>
>>>>why somebody wants to write such thing
>>>>struct abc {
>>>>    __u16 a;
>>>>    __u16 b;
>>>>    char abc[0];
>>>>} __attribute__((packed));
>>>>Specialy for zerod array of chars.
>>>>        
>>>>
>>>A structure which ends with a zero-length array is intended for use as
>>>the "header" for a variable-sized block of data, e.g.:
>>>
>>>	struct abc *new_abc(__u16 a, __u16 b, const char *string)
>>>	{
>>>		int len = strlen(string);
>>>		struct abc *p = malloc(sizeof(struct abc) + len + 1);
>>>	
>>>		p->a = a;
>>>		p->b = b;
>>>		memcpy(p->abc, string, len + 1);
>>>	
>>>		return p;
>>>	}
>>>
>>>  
>>>      
>>>
>>So if my compiler can't handle such code, can i change it to
>>struct abc {
>>    __u16 a;
>>    __u16 b;
>>    char abc[1];
>>} __attribute__((packed));
>>    
>>
>
>Yes, although you would need to adjust the size calculation if you
>need to calculate the exact size of the overall block.
>
>Zero-length arrays aren't valid in C89, but are in C99. Also, gcc has
>supported them as an extension since long before it started supporting
>C99.
>
>  
>
So char abc[0] actualy pointer to chat? It should be 4 byte long on x86.
I can't see it with sizeof.

  reply	other threads:[~2005-05-16  4:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-15  9:32 data structure question gumbold
2005-05-15 11:02 ` Glynn Clements
2005-05-15 11:24   ` gumbold
2005-05-15 14:23     ` Glynn Clements
2005-05-16  4:00       ` gumbold [this message]
2005-05-16 15:28         ` Glynn Clements
2005-05-16 18:31           ` gumbold
2005-05-17  7:36             ` Glynn Clements
2005-05-16 19:07           ` gumbold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42881ADE.2010009@bonbon.net \
    --to=gumbold@bonbon.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.