VecForeachPtr
- Macro
- August 22, 2025
Table of Contents
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) * ). |
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)
- In
Foreach.h:113
:
/// body : The block of code to be executed for each character of the Str.
///
#define StrForeachPtr(str, chrptr, body) VecForeachPtr((str), (chrptr), {body})
///
- In
Str.c:866
:
void ValidateStrs(const Strs* vs) {
VecForeachPtr(vs, sp, { ValidateStr(sp); });
}
- In
Main.c:9
:
// 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)