Functions

ATail

Return the last element of an array

Syntax

Signature
ITEM *ATail( const ITEM *array );

Arguments

array Array whose last element is wanted.

Return value

A borrowed pointer to the last element, or NULL if the array is empty or the argument is not an array.

Description

Read the value in the final slot without calculating its index. This is equivalent to accessing the element at the current array length. A nonempty array may have a `NIL` value in its last slot. Use `ItemType()` to inspect that value.

A `NULL` result means there is no final slot. A returned pointer belongs to the array and should be used before resizing or releasing it.

Example

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

#include <stdio.h>

int main( void )
{
   ITEM *stops = Array( 3 );

   if( !stops )
      return 1;

   ArraySetC( stops, 1, "Gdansk" );
   ArraySetC( stops, 2, "Warsaw" );
   ArraySetC( stops, 3, "Krakow" );

   const ITEM *last = ATail( stops );

   if( last )
      printf( "Last destination: %s\n", ItemGetCPtr( last ) );

   ItemRelease( stops );

   return 0;
}

Output

Last destination: Krakow

See also