How would one implement Lazy Evaluation in C?

后端 未结 9 1983
野的像风
野的像风 2021-02-02 13:24

Take for example,

The follow python code:

def multiples_of_2():
  i = 0
  while True:
    i = i + 2
    yield i

How do we translate thi

9条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 13:57

    You can pass the argument as a pointer to allow the function to modify it without using the return value:

    void multiples_of_2(int *i)
    {
        *i += 2;
    }
    

    And call it:

    int i = 0;
    multiples_of_2(&i);
    

提交回复
热议问题