Skip to content

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.

Parameters

Name Direction Description
l in List to iterate over.
var out Name of the variable to be used which will contain the value of the current element during iteration. The type of var will be the data type of the list elements (obtained via LIST_DATA_TYPE(l)).

Success

The loop body runs once for each node from head to tail with var bound to a copy of that node’s data. The body is skipped when l is empty. The list is not modified by the macro itself.

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 (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeach(list, item) {
                        sum += item;
                    }
        typedef List(int) LI;
        LI li = ListInit(ALLOCATOR_OF(&alloc));
        ListForeach(&li, i) {
            (void)i;
        }
        FILL_INT_LIST(&list);
    
        ListForeach(&list, value) {
            forward_sum += value;
        }
        u64     count = 0;
    
        ListForeach(&list, value) {
            (void)value;
            count += 1;
Last updated on