ListForeachIdx
- Macro
- October 8, 2025
Table of Contents
ListForeachIdx
ListForeachIdx
Description
Iterate over each element var
of the given list l
, with index idx
. The variable var
is declared and defined by this macro. 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 contain a copy of the value pointed to by each 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 | Name of the variable that will hold the current value during iteration. |
idx | out | Name of the variable that will hold the current index during iteration. |
Usage example (Cross-references)
- In
ListInt.c:224
:
if (list->length > 0) {
int sum = 0;
ListForeachIdx(list, item, idx) {
sum += item + (int)idx;
}