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.
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. |
Success
The loop body runs once for each node from head to tail with idx advancing from 0 to l->length - 1 and var bound to the in-node data address. Use this form when the body mutates elements in place. The body may reassign idx to perform random access. The body is skipped when l is empty.
Failure
The macro itself does not fail. LOG_FATAL via ValidateList(l) when l is uninitialised or corrupted.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
ListInt.c:253:
if (ListLen(list) > 0) {
int sum = 0;
ListForeachPtrIdx(list, item_ptr, idx) {
sum += *item_ptr + (int)idx;
} }
ListForeachPtrIdx(&list, value_ptr, idx) {
*value_ptr += (int)idx;
} }
ListForeachPtrIdx(&list, value_ptr, idx) {
*value_ptr += (int)idx;
if (idx == 0) { }
ListForeachPtrIdx(&list, value_ptr, idx) {
(void)value_ptr;
(void)idx;