ListForeachPtrIdx
- Macro
- October 8, 2025
Table of Contents
ListForeachPtrIdx
ListForeachPtrIdx
Description
Iterate over each element var
(as a pointer) of the given list l
, with index idx
. The variable var
is declared and defined by this macro as a pointer to the data. Iteration happens in forward order, starting from the head of the list. This macro also tracks the index (idx
) of each element during iteration. var
will point to the data associated with the current list node, and idx
will be the zero-based index of the current element.
Info
The macro supports iteration using relative traversal if random access is required. This means user code can change idx
to any value in list boundaries and the macro will adjust node automatically for the new index.
Parameters
Name | Direction | Description |
---|---|---|
l | in | List to iterate over. |
var | out | Pointer variable that will point to the current element. |
idx | out | Name of the variable that will hold the current index during iteration. |
Usage example (Cross-references)
- In
ListInt.c:246
:
if (list->length > 0) {
int sum = 0;
ListForeachPtrIdx(list, item_ptr, idx) {
sum += *item_ptr + (int)idx;
}