ListForeachIdx

Table of Contents

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

NameDirectionDescription
linList to iterate over.
varoutName of the variable that will hold the current value during iteration.
idxoutName of the variable that will hold the current index during iteration.

Usage example (Cross-references)

    if (list->length > 0) {
    int sum = 0;
    ListForeachIdx(list, item, idx) {
    sum += item + (int)idx;
    }

Share :

Related Posts

ListForeachPtrReverseInRange

ListForeachPtrReverseInRange Description Iterate over each element var (as a pointer) of the given list l in reverse, limited to index range [start, end) relative to the tail of the list. Index 0 corresponds to the tail, 1 to the previous node, and so on. The variable var is declared and defined by this macro as a pointer to the list’s data type. Since linked lists do not support indexing, this macro counts nodes from the tail and includes only those where the relative reverse index lies in [start, end).

Read More

ListForeach

ListForeach Description Iterate over each element var of the given list l. The variable var is declared and defined by this macro. Iteration happens in forward order, starting from the head of the list and continuing through the next pointers until the end is reached. The variable var will contain a copy of the value pointed to by each list node.

Read More

VecForeachPtrInRangeIdx

VecForeachPtrInRangeIdx Description Iterate over elements in a specific range of the given vector v at each index idx (as pointers). The variables var and idx are declared and defined by this macro. idx will start from start and will go till end - 1

Read More