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) *).

Usage example (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))
    
    ///

Share :

Related Posts

bool

bool Description Function pointer type for test functions.

Read More

VecForeachReverse

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.

Read More

VecForeach

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

Read More