Skip to content

VecForeachPtr

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

Name Direction Description
v in,out Vector to iterate over.
var in Name 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) *).

Usage example (Cross-references)

Usage examples (Cross-references)
                if (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachPtr(vec, str_ptr) {
                        total_len += strlen(*str_ptr);
                    }
                if (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachPtr(vec, str_ptr) {
                        total_len += strlen(str_ptr->data);
                    }
                if (VecLen(vec) > 0) {
                    int sum = 0;
                    VecForeachPtr(vec, item_ptr) {
                        sum += *item_ptr;
                    }
    void ValidateStrs(const Strs *vs) {
        ValidateVec(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) {
        WriteFmt("Testing VecForeachPtr\n");
    
        // Create a vector of integers
    
        // Use VecForeachPtr to modify the values in the vector
        VecForeachPtr(&vec, item_ptr) {
            *item_ptr *= 2;
        }
        // Use VecForeachPtr to calculate sum
        int sum = 0;
        VecForeachPtr(&vec, item_ptr) {
            sum += *item_ptr;
        }
    ///               implementation (e.g., `char*`).
    ///
    #define StrForeachPtr(str, chrptr) VecForeachPtr((str), (chrptr))
    
    ///
Last updated on