Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Vincent Lefevre Newsgroups: comp.std.c Subject: May a string span multiple, independent objects? Date: Wed, 3 Jul 2024 14:31:27 -0000 (UTC) Organization: a training zoo Lines: 43 Message-ID: <20240703141500$00ed@vinc17.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 03 Jul 2024 16:31:27 +0200 (CEST) Injection-Info: dont-email.me; posting-host="579583abec043e4c8fe1caec9afd78cd"; logging-data="2342025"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18PTcrrezDfFkZw+0F3ng0e" User-Agent: tin/2.6.4-20240531 ("Banff") (Linux/6.7.12-amd64 (x86_64)) Cancel-Lock: sha1:wB8b5TAtajHHBnLu+Z1J6FUwbmU= Xref: csiph.com comp.std.c:6646 ISO C17 (and C23 draft) 7.1.1 defines a string as follows: "A string is a contiguous sequence of characters terminated by and including the first null character." But may a string span multiple, independent objects that happens to be contiguous in memory? For instance, is the following program valid and what does the ISO C standard say about that? #include #include typedef char *volatile vp; int main (void) { char a = '\0', b = '\0'; vp p = &a, q = &b; printf ("%p\n", (void *) p); printf ("%p\n", (void *) q); if (p + 1 == q) { a = 'x'; printf ("%zd\n", strlen (p)); } if (q + 1 == p) { b = 'x'; printf ("%zd\n", strlen (q)); } return 0; } If such a program is valid, would there be issues by working with pointers on such a string, say, dereferencing p[1] in the first "if" (which is normally UB)? -- Vincent Lefèvre - Web: 100% accessible validated (X)HTML - Blog: Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)