Functions

Array

Create a one-dimensional or multidimensional array

Syntax

Signature
ITEM *Array( int elements, ... );

Arguments

elements Length of the first dimension. Additional int arguments give the lengths of the remaining dimensions.

Return value

A new array owned by the caller, or NULL if a dimension is negative or the call specifies more than 64 dimensions.

Description

`Array( 0 )` creates an empty array that `AAdd()` or `ASize()` can grow. With multiple dimensions, each number gives the length of one nesting level. For example, `Array( 3, 2 )` creates three separate rows with two `IT_NIL` elements each.

Use one-based indices. `ArrayGetItemPtr()` retrieves a row, and the typed `ArraySet*()` and `ArrayGet*()` functions access its elements. The macro in `xdev-wtui.h` adds the `NIL` terminator automatically. Release the outer item with `ItemRelease()` to release its ownership of the rows.

Example

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

#include <stdio.h>

int main( void )
{
   ITEM *stock = Array( 3, 2 );

   if( !stock )
      return 1;

   const char *names[] = { "Notebook", "Pencil", "Eraser" };
   const int quantities[] = { 12, 30, 8 };

   for( size_t i = 0; i < sizeof( names ) / sizeof( names[ 0 ] ); ++i )
   {
      ITEM *row = ArrayGetItemPtr( stock, i + 1 );

      ArraySetC( row, 1, names[ i ] );
      ArraySetNI( row, 2, quantities[ i ] );
      printf( "%s: %d\n", ArrayGetCPtr( row, 1 ), ArrayGetNI( row, 2 ) );
   }

   ItemRelease( stock );

   return 0;
}

Output

Notebook: 12
Pencil: 30
Eraser: 8

See also