Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: "array" Date: Wed, 02 Apr 2025 18:31:30 -0700 Organization: None to speak of Lines: 33 Message-ID: <85a58y58ul.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 03 Apr 2025 03:31:31 +0200 (CEST) Injection-Info: dont-email.me; posting-host="658736fab9029f452335cecd036b8387"; logging-data="3447921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/O7r3pyILF/DovOnINit9Z" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:rXbsooaT/WqKTe4EMtusuKSczEs= sha1:fWi/1xznPxuTe6EFW9QaS+EMkn0= Xref: csiph.com comp.lang.c:391870 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Below, an array is allocated dynamically. > > #include > #include > > int main( void ) > { char *array_pointer = malloc( 10 * sizeof *array_pointer ); > if( !array_pointer )return EXIT_FAILURE; > *array_pointer = 'a'; > free( array_pointer ); } > > But is it really an array according to the C spec? Yes. This is specfied by the standard in the section describing memory allocation functions. In C17, it's in 7.22.3 paragraph 1 (which applies to all of aligned_alloc, calloc, malloc, and realloc): The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The *effective type* rules are also relevant (section 6.5). My reading of that section is that if you access malloc'ed memory as an array, that memory has the array type as its effective type. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */