VecForeachPtr

Table of Contents

VecForeachPtr

Description

Iterate over each element var of the given vector v (as a pointer). This is a convenience macro that iterates forward 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 beginning to the end.

Failure

If the vector v is NULL or its length is zero, the loop body will not be executed. Any failures within the VecForeachPtrIdx 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 StrForeachPtr(str, chrptr, body) VecForeachPtr((str), (chrptr), {body})
    
    ///
    
    void ValidateStrs(const Strs* vs) {
    VecForeachPtr(vs, sp, { ValidateStr(sp); });
    }
    
    // Use the fixed VecForeachPtr macro
    VecForeachPtr(&lines, line, {
    if (StrStartsWithZstr(line, "[.") && StrEndsWithZstr(line, "]")) {
    Str rule_name = StrInit();
    // Test VecForeachPtr macro
    bool test_vec_foreach_ptr(void) {
    printf("Testing VecForeachPtr\n");
    
    // Create a vector of integers
    
    // Use VecForeachPtr to modify the values in the vector
    VecForeachPtr(&vec, item_ptr, { *item_ptr *= 2; });
    
    // Check that the values in the vector are doubled
    // Use VecForeachPtr to calculate sum
    int sum = 0;
    VecForeachPtr(&vec, item_ptr, { sum += *item_ptr; });
    
    // Check the sum (should be doubled values)

Share :