Functions

AAdd

Append a value to an array

Syntax

Signature
const ITEM *AAdd( ITEM *array, const ITEM *value );

Arguments

array Destination array.

value Value to copy into the new element. Pass NULL to append a NIL element.

Return value

The supplied value pointer, or NULL if array is not an array. A successful call with a NULL value also returns NULL.

Description

Append one value to the array. This grows the original array and is useful for building lists whose final length is unknown. The call stores its own item value, so the caller can release a separately allocated source item after the append.

An array value is stored by reference. Create a fresh subarray for each independent row, or use `AClone()` to duplicate an existing row. An append can move the array's element storage. Obtain fresh element pointers after adding an item.

Example

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

#include <stdio.h>

int main( void )
{
   ITEM *stock = Array( 0 );
   ITEM *row = Array( 2 );

   if( !stock || !row )
   {
      ItemRelease( row );
      ItemRelease( stock );
      return 1;
   }

   ArraySetC( row, 1, "Notebook" );
   ArraySetNI( row, 2, 12 );
   AAdd( stock, row );

   /* a new array for the next row; stock keeps the first one */
   ArrayNew( row, 2 );
   ArraySetC( row, 1, "Pencil" );
   ArraySetNI( row, 2, 30 );
   AAdd( stock, row );
   ItemRelease( row );

   for( int64_t i = 1; i <= ArrayLen( stock ); ++i )
   {
      const ITEM *entry = ArrayGetItemPtr( stock, ( size_t )i );

      printf( "%s: %d\n", ArrayGetCPtr( entry, 1 ), ArrayGetNI( entry, 2 ) );
   }

   ItemRelease( stock );

   return 0;
}

Output

Notebook: 12
Pencil: 30

See also