About implementing foreach in C language

Asked 2 years ago, Updated 2 years ago, 32 views

I was wondering how to do an array like foreach in C language, so I looked it up and found out that
I found a page that introduced macros like the one below.

C language:Using foreach macros

#defineforeach(item, array)\
for (int keep = 1, \
    count = 0, \
    size=sizeof(array)/sizeof*(array);\
    keep&count!=size;\
    keep=!keep, count++)\
    for(item=(array)+count;keep;keep=!keep)\

When I tried to move the above code, it worked as expected.
As I tried to understand, I have two questions.

I understand that this macro is defined in the Linux Kernel. Does anyone know which file it is defined in?

I don't know why the first for conditional expression for this macro requires keep&&.
I understand that keep is a necessary condition to run the second for only once, but then I don't think it's possible for keep to be 0 when evaluating the conditional expression of the first for.
What is the purpose of this statement?

I'm sorry if I misread your question and asked you a random question.

c

2022-09-29 21:28

4 Answers

Maybe the reason why we are testing keep on the outer for loop is because when we break the inner for loop, the outer for loop also comes out?


2022-09-29 21:28

Regarding 2, I think it's to escape by breaking, but it's supplementary

The normal process is

However, if there is a break in the loop,

and the outer loop can also escape.

You flip keep to each other in the update block of the outer for and inner for, and if there is an inconsistency, break the whole thing and escape.


2022-09-29 21:28

Let me answer about 1.There seemed to be no such foreach macro implemented in the Linux 5.2.13 source code at hand.

Download the 2013 kernel source code from kernel.org to find something.


2022-09-29 21:28

[Additional note] I tried orangecat's answer.Considering the break, keep&count!=size; was the action needed to get out of the first for loop.

US>2. I don't know why the first for conditional expression for this macro requires keep&&. Keep is the second for
I understand that is a requirement to run only once, so keep is 0
in the first for conditional expression evaluation. I don't think it's possible that ... What is the purpose of this keep&&?

I also think keep is a condition to run the second for loop only once.
keep&&count!=size;\ does not require keep&&.
I don't think it's a problem to shave it.

I tried to shave the keep and changed the macro to run the second for loop only once.

#defineforeach(item, array)\
for(\
    int count = 0, \
    size=sizeof(array)/sizeof*(array);\
    count!=size;\
    count++)\
    for(item=(array)+count, i=0;i<1;i++)\

This code is problematic because the control variable i assumes the same type as the item, but for each element of array, it repeats the minutes that follow foreach for a few minutes.Keep is independent of the process of repeating itself.

Below are the verification codes and results.
[Code]

#include<stdio.h>
# define foreach(item, array)\
for (int keep = 1, \
    count = 0, \
    size=sizeof(array)/sizeof*(array);\
    keep&count!=size;\
    keep=!keep, count++)\
    for(item=(array)+count;keep;keep=!keep)\

#defineforeach2(item, array)\
for(\
    int count = 0, \
    size=sizeof(array)/sizeof*(array);\
    count!=size;\
    count++)\
    for(item=(array)+count, i=0;i<1;i++)\


int main(void){
    int values[] = {10,20,30,40,50};
    foreach(int*v, values) {
        printf("value:%d\n", *v);
    }
    printf("------------------------------\n";
    foreach2(int*v, values) {
        printf("value:%d\n", *v);
    }
    return 0;
}

[Results]

$clang-Weverything qw.c;
$ ./a.out
value —10
value —20
value —30
value —40
value —50
--------------------------
value —10
value —20
value —30
value —40
value —50


2022-09-29 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.