Skip to content
ListForeachPtrInRange

ListForeachPtrInRange

Description

This macro performs forward traversal, starting at index start (inclusive) and continuing until index end (exclusive), assuming zero-based indexing.

Since linked lists are not indexable, the traversal walks node-by-node and skips nodes before start, then continues while tracking the current index.

Parameters

Name Direction Description
l in,out List to iterate over.
var in,out Name of the pointer variable to be used which will point to the current element during iteration.
start in Starting index (inclusive).
end in Ending index (exclusive).

Success

The loop body runs once for each node whose head-relative index lies in [start, end), with var bound to the in-node data address. Use this form when the body mutates elements in place. The body is skipped when the range or 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)
                    if (start < end) {
                        int sum = 0;
                        ListForeachPtrInRange(list, item_ptr, start, end) {
                            sum += *item_ptr;
                        }
        }
    
        ListForeachPtrInRange(&list, value_ptr, 1, 4) {
            *value_ptr *= 2;
        }
        }
    
        ListForeachPtrInRange(&list, value_ptr, 7, 9) {
            (void)value_ptr;
            count += 10;
        }
    
        ListForeachPtrInRange(&list, value_ptr, 0, 1) {
            (void)value_ptr;
            count += 100000;
Last updated on