Functions

AIns

Insert a NIL element while keeping the array length

Syntax

Signature
ITEM *AIns( ITEM *array, int position );

Arguments

array Array to edit.

position One-based position at which to insert an element.

Return value

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

Description

Open a slot at the given one-based position. The new slot contains `NIL`. Following values shift one position toward the end, and the original last value is released. The length stays the same.

Use `ASize()` first to preserve the last value when inserting into a full array. An out-of-range position leaves the array unchanged.

Example

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

#include <stdio.h>

int main( void )
{
   ITEM *agenda = Array( 2 );

   if( !agenda )
      return 1;

   ArraySetC( agenda, 1, "First" );
   ArraySetC( agenda, 2, "Third" );

   ASize( agenda, 3 );
   AIns( agenda, 2 );
   ArraySetC( agenda, 2, "Second" );

   for( int64_t i = 1; i <= ArrayLen( agenda ); ++i )
      puts( ArrayGetCPtr( agenda, ( size_t )i ) );

   ItemRelease( agenda );

   return 0;
}

Output

First
Second
Third

See also