From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ai7A9-0004Hd-2R for qemu-devel@nongnu.org; Mon, 21 Mar 2016 17:14:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ai7A8-0001Pd-0i for qemu-devel@nongnu.org; Mon, 21 Mar 2016 17:14:56 -0400 References: <1457502997-30904-1-git-send-email-peterx@redhat.com> <1457502997-30904-2-git-send-email-peterx@redhat.com> <56E0A02B.50102@redhat.com> <20160310014625.GE4091@pxdev.xzpeter.org> From: Eric Blake Message-ID: <56F06448.50308@redhat.com> Date: Mon, 21 Mar 2016 15:14:48 -0600 MIME-Version: 1.0 In-Reply-To: <20160310014625.GE4091@pxdev.xzpeter.org> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ihRQv2UnpkA8MX356UDXu6opMar15R9RP" Subject: Re: [Qemu-devel] [PATCH 1/2] block/qapi: make two printf() formats literal List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Xu Cc: kwolf@redhat.com, pbonzini@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org, armbru@redhat.com This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --ihRQv2UnpkA8MX356UDXu6opMar15R9RP Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 03/09/2016 06:46 PM, Peter Xu wrote: > On Wed, Mar 09, 2016 at 03:14:03PM -0700, Eric Blake wrote: >>> + func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, >>> + composite ? '\n' : ' '); >> >> [The nerd in me wants to point out that you could avoid the ternary by= >> writing '"\n "[composite]', but that's too ugly to use outside of IOCC= C >> submissions, and I wouldn't be surprised if it (rightfully) triggers >> clang warnings] >=20 > Do you mean something like: >=20 > int i =3D 0; > printf("%c", '"\n "[i]'); You mean: printf("%c", "\n "[i]); (no ''). But with your declaration of 'i' as int, it is only defined if i <=3D 2 (whereas in my example above, "\n "[composite] is always defined= because composite is bool rather than int). >=20 > Is this a grammar btw? Yes, C has an ugly grammar, because [] is just syntactic sugar for deferencing pointer addition with nicer operator precedence. Quoting C99 6.5.2.1: "The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero)." And a string literal is just a fancy way of writing the address of an array of characters (where the address is chosen by the compiler). Thus, it IS valid to dereference the addition of an integer offset with the address implied by a string literal in order to obtain a character within the string. And since the [] operator is commutative (even though no one in their right mind commutes the operands), you can also write the even-uglier: composite["\n "] But now we've gone far astray from the original patch review :) --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --ihRQv2UnpkA8MX356UDXu6opMar15R9RP Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJW8GRJAAoJEKeha0olJ0NqjtYH/3K9/r/mo5jBXQRGlwj8LHaY rSqCVyTOKxM8oEqXxrqh98H/lmgOioyVNvz10TCJ4cn5/bJDKvrkLQaKjMgelQM1 wMLu7788t1P9tWMptvz450uxSjqrFMdMdW72gjuNsUQfKQhXzd6X+wibush1dMEk 3lx/t1T1K0lu2kKhWXOrWG0JqlQ5fin5ooSsXufqy5l8KtrsN8M5jtqyHNABX+z8 1as076WjC1+b31iYCH9osFT/PBk530b93KrrFPcCfRDZFi3VKtz8qW0u8fNM6ous SwcOQI+2YvOmeWFh9+5gqOPoid3QDUmRCtAgSPJjhCv8Tq1u6P5LYYeUhLC9oLE= =pxJ4 -----END PGP SIGNATURE----- --ihRQv2UnpkA8MX356UDXu6opMar15R9RP--