VecForeachPtrReverse

Table of Contents

VecForeachPtrReverse

Description

Iterate over each element var (as a pointer) of the given vector v in reverse order. This is a convenience macro that iterates backward using an internally managed index and provides a pointer to each element. The variable var is declared and defined by this macro as a pointer to the vector’s data type.

Parameters

NameDirectionDescription
vin,outVector to iterate over.
varinName of the pointer variable to be used which will point to the current element during iteration. The type of var will be a pointer to the data type of the vector elements (obtained via VEC_DATATYPE(v) *).

Success

The body is executed for each element of the vector v (with var pointing to the current element) from the end to the beginning.

Failure

If the vector v is NULL or its length is zero, the loop body will not be executed. Any failures within the VecForeachPtrReverseIdx macro (like invalid index access) will result in a fatal log message and program termination.

Usage example (Cross-references)

    /// body        : The block of code to be executed for each character of the Str.
    ///
    #define StrForeachPtrReverse(str, chrptr, body) VecForeachPtrReverse((str), (chrptr), {body})
    
    ///
    // Test VecForeachPtrReverse macro
    bool test_vec_foreach_ptr_reverse(void) {
    printf("Testing VecForeachPtrReverse\n");
    
    // Create a vector of integers
    // Use VecForeachPtrReverse to increment values in reverse order
    int increment = 1;
    VecForeachPtrReverse(&vec, item_ptr, { *item_ptr += increment++; });
    
    // Values should now be: [15, 24, 33, 42, 51]

Share :