VecForeachPtr
- Macro
- October 8, 2025
Table of Contents
VecForeachPtr
VecForeachPtrDescription
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)
- In
Str.c:864:
void ValidateStrs(const Strs *vs) {
ValidateVec(vs);
VecForeachPtr(vs, sp) {
ValidateStr(sp);
}
// 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;
}
- In
Main.c:9:
// Use the fixed VecForeachPtr macro
VecForeachPtr(&lines, line) {
if (StrStartsWithZstr(line, "[.") && StrEndsWithZstr(line, "]")) {
Str rule_name = StrInit();
- In
Foreach.h:106:
/// implementation (e.g., `char*`).
///
#define StrForeachPtr(str, chrptr) VecForeachPtr((str), (chrptr))
///
- In
VecInt.c:421:
if (VecLen(vec) > 0) {
int sum = 0;
VecForeachPtr(vec, item_ptr) {
sum += *item_ptr;
}
- In
VecStr.c:451:
if (VecLen(vec) > 0) {
size_t total_len = 0;
VecForeachPtr(vec, str_ptr) {
total_len += strlen(str_ptr->data);
}
- In
VecCharPtr.c:489:
if (VecLen(vec) > 0) {
size_t total_len = 0;
VecForeachPtr(vec, str_ptr) {
total_len += strlen(*str_ptr);
}