gcc_init_compound_literal.c revision 1.8
11.8Srillig/*	$NetBSD: gcc_init_compound_literal.c,v 1.8 2023/07/29 07:49:15 rillig Exp $	*/
21.1Srillig# 3 "gcc_init_compound_literal.c"
31.1Srillig
41.1Srillig/*
51.1Srillig * C99 says in 6.7.8p4:
61.1Srillig *
71.1Srillig *	All the expressions in an initializer for an object that has static
81.1Srillig *	storage duration shall be constant expressions or string literals.
91.1Srillig *
101.2Srillig * The term "constant expression" is defined in C99 6.6, where 6.6p9 allows
111.2Srillig * "constant expressions" in initializers to also be an "address constant".
121.2Srillig * Using these address constants, it is possible to reference an unnamed
131.2Srillig * object created by a compound literal (C99 6.5.2.5), using either an
141.2Srillig * explicit '&' or the implicit array-to-pointer conversion from C99 6.3.2.1.
151.3Srillig *
161.8Srillig * Before init.c 1.195 from 2021-04-17, an assertion in check_global_variable
171.8Srillig * failed since these temporary objects have neither storage class EXTERN nor
181.8Srillig * STATIC.
191.1Srillig */
201.1Srillig
211.7Srillig/* lint1-extra-flags: -X 351 */
221.7Srillig
231.1Srillig// Seen in sys/crypto/aes/aes_ccm.c.
241.1Srilligconst struct {
251.1Srillig    const unsigned char *ctxt;
261.1Srillig} T = {
271.3Srillig	.ctxt = (const unsigned char[4]){
281.3Srillig	    1, 2, 3, 4
291.3Srillig	},
301.1Srillig};
311.2Srillig
321.2Srilligstruct node {
331.2Srillig	int num;
341.2Srillig	struct node *left;
351.2Srillig	struct node *right;
361.2Srillig};
371.2Srillig
381.2Srillig/*
391.2Srillig * Initial tree for representing the decisions in the classic number guessing
401.2Srillig * game often used in teaching the basics of programming.
411.2Srillig */
421.6Srillig/* expect+1: warning: static variable 'guess' unused [226] */
431.2Srilligstatic const struct node guess = {
441.2Srillig	50,
451.2Srillig	&(struct node){
461.2Srillig		25,
471.2Srillig		&(struct node){
481.2Srillig			12,
491.2Srillig			(void *)0,
501.2Srillig			(void *)0,
511.2Srillig		},
521.2Srillig		&(struct node){
531.2Srillig			37,
541.2Srillig			(void *)0,
551.2Srillig			(void *)0,
561.2Srillig		},
571.2Srillig	},
581.2Srillig	(void *)0
591.2Srillig};
60