Functions

ASize

Grow or shrink an array

Syntax

Signature
ITEM *ASize( ITEM *array, int length );

Arguments

array Array to resize.

length New length, including 0 for an empty array.

Return value

The array, or NULL if array is not an array.

Description

Resize the original array to the requested length. Growth adds `NIL` slots, while shrinking releases values beyond the new end. Resizing to `0` leaves an empty array that can be used again. A negative length leaves the array unchanged.

The element storage can move. Obtain fresh element pointers after resizing.

Example

asize.c
#include "xdev-wtui.h"

#include <inttypes.h>
#include <stdio.h>

int main( void )
{
   ITEM *array = Array( 1 );

   if( !array )
      return 1;

   ArraySetNI( array, 1, 42 );
   ASize( array, 3 );
   printf( "After growing: %" PRId64 " slots, last is %s\n",
           ArrayLen( array ), ItemType( ATail( array ) ) == IT_NIL ? "NIL" : "set" );

   ASize( array, 1 );
   printf( "After shrinking: %" PRId64 " slot, last is %d\n",
           ArrayLen( array ), ItemGetNI( ATail( array ) ) );

   ItemRelease( array );

   return 0;
}

Output

After growing: 3 slots, last is NIL
After shrinking: 1 slot, last is 42

See also