From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9182 invoked by alias); 8 Jan 2014 20:41:59 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 9159 invoked by uid 89); 8 Jan 2014 20:41:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-qa0-f51.google.com Received: from mail-qa0-f51.google.com (HELO mail-qa0-f51.google.com) (209.85.216.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 08 Jan 2014 20:41:58 +0000 Received: by mail-qa0-f51.google.com with SMTP id i13so2297419qae.10 for ; Wed, 08 Jan 2014 12:41:55 -0800 (PST) X-Received: by 10.49.75.137 with SMTP id c9mr13943285qew.27.1389213715714; Wed, 08 Jan 2014 12:41:55 -0800 (PST) Received: from localhost.localdomain ([181.66.233.189]) by mx.google.com with ESMTPSA id w7sm2766982qaj.23.2014.01.08.12.41.53 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 08 Jan 2014 12:41:54 -0800 (PST) Message-ID: <52CDB80F.2080807@gmail.com> Date: Wed, 08 Jan 2014 20:41:00 -0000 From: Ian Pilcher User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Jonathan Wakely CC: gcc-help Subject: Re: Flexible array member initializers References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2014-01/txt/msg00022.txt.bz2 On 01/08/2014 03:59 AM, Jonathan Wakely wrote: > The member def_value has no storage, so you can't initialize it. To > use it you need to allocate sizeof(struct foo_opt)+sizeof(x) where x > is the value you want def_val to have, and when you access def_val you > will be accessing the extra sizeof(x) bytes after the struct. > > I don't see how you can statically initialize a foo_opt when you don't > know sizeof(x). > Here's a very simplified example for the specific case of a 16-bit short on a little-endian system: #define SHORT_BYTES(s) { ((s) & 0xff), (((s) >> 8) & 0xff) } struct foo { size_t value_size; unsigned char value[]; }; static struct foo foo_short = { .value_size = sizeof(short), .value = SHORT_BYTES(513), }; Ideally I could create some sort of generic macro that "casts" (using the term very loosely) any static initializer to an array of unsigned char that contains its binary representation. Sadly, I don't think that's actually possible. I don't supposed that gcc provides any facility that will allow the pre-processor to determine endianness and/or type sizes? -- ======================================================================== Ian Pilcher arequipeno@gmail.com "If you're going to shift my paradigm ... at least buy me dinner first." ========================================================================