Tag: 迭代

每次递归都可以改为迭代吗?

每个递归函数都可以转换为迭代吗? 递归函数应该具有什么特性才能使用迭代实现它? 我一直在尝试使用迭代定义以下函数,但似乎是不行! 它应该探索迷宫中的所有路径(节点)。 任何人都可以使用迭代重写这个吗? 如果不可能,为什么不呢? typedef int[0,99] id_t; bool visited[id_t]; int path[id_t]; int pathCounter = 0; struct { id_t id; bool free; int neighborNode[4]; } nodeMap[id_t]; void findPath(int current){ visited[current] = true; for (i : int[0, 3]){ if(nodeMap[nodeMap[current].neighborNode[i]].free == true && visited[nodeMap[current].neighborNode[i]] == false && nodeMap[current].neighborNode[i] != -1){ path[pathCounter] = nodeMap[nodeMap[current].neighborNode[i]].id; pathCounter++; findPath(nodeMap[current].neighborNode[i]); path[pathCounter] […]

用于迭代像数组一样的struct成员的C方法?

假设我有一个矢量类: typedef struct vec3_s { float x, y, z; } vec3; 但是,我希望能够迭代它而不将其转换为浮点数组。 虽然在这种情况下演员阵容是可以接受的,但我很想知道在C ++中是否有任何类似function的东西在C语言中是可行的。例如,在C ++中,因为std::vector具有subscript []运算符重载,我可以将其第一个索引的地址传递给一个带void*的函数void* 。 即 void do_something_with_pointer_to_mem( void* mem ) { // do stuff } int main( void ) { std::vector v; // fill v with values here // pass to do_something_with_pointer_to_mem do_some_with_pointer_to_mem( &v[ 0 ] ); return; } 另一个更具体的例子是在OpenGL中使用glBufferData(…)时(使用C ++时): glBufferData( […]

迭代C中相同类型的struct成员

是否可以使用指针迭代C结构,其中所有成员都是相同类型。 这是一些不编译的示例代码: #include #include typedef struct { int mem1 ; int mem2 ; int mem3 ; int mem4 ; } foo ; void my_func( foo* data ) { int i ; int* tmp = data ; // This line is the problem for( i = 0; i < 4; ++i ) { ++tmp ; printf( […]