Sujet : "array"
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.cDate : 02. Apr 2025, 12:01:01
Autres entêtes
Organisation : Stefan Ram
Message-ID : <array-20250402114422@ram.dialup.fu-berlin.de>
Below, an array is allocated dynamically.
#include <stdio.h>
#include <stdlib.h>
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?
C only defines "array type", not array, but it uses ISO/IEC 2382:2015
as a normative reference, and ISO/IEC 2382:2015 says:
|array
|An aggregate that is an instance of an array type and each
|element or appropriate group of elements in which may be
|referenced randomly and independently of the others.
ISO/IEC 2382-15 (1998), 15.03.08 (Can't access newer versions!)
. That block of memory that malloc allocated, does it have an
array type? I'm not sure. So are we allowed to call it an array?
I think the C community /would/ mostyly call this an array.
Here's an attempt to give it an explicit array type:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{ char( *array_pointer )[ 10 ]= malloc( sizeof *array_pointer );
if( !array_pointer )return EXIT_FAILURE;
( *array_pointer )[ 0 ]= 'a';
free( array_pointer ); }
Is it now more of an array than before?
In n3467:
|The effective type of an object that is not a byte array, for
|an access to its stored value, is the declared type of the
|object. 83)
from 6.5p6
|83) Allocated objects have no declared type.
footnote for 6.5p6
|For all other accesses to a byte array, the effective type of
|the object is simply the type of the lvalue used for the access.
from 6.5p6
In my example programs, the lvalue for the access has the
type "int". So, I can see that there's an int object, and
- in a similar way - that there are other int objects behind it,
but not necessarily that they form an array.