VecForeachReverse

Table of Contents

VecForeachReverse

Description

Iterate over each element var of the given vector v in reverse order. This is a convenience macro that iterates backward using an internally managed index. The variable var is declared and defined by this macro.

Parameters

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

Success

The body is executed for each element of the vector v 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 VecForeachReverseIdx 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 StrForeachReverse(str, chr, body) VecForeachReverse((str), (chr), {body})
    
    ///
    // Test VecForeachReverse macro
    bool test_vec_foreach_reverse(void) {
    printf("Testing VecForeachReverse\n");
    
    // Create a vector of integers
    int reversed[5] = {0};
    int idx         = 0;
    VecForeachReverse(&vec, item, { reversed[idx++] = item; });
    
    // Check that the reversed array is correct

Share :