Is it possible to iterate of a C struct, where all members are of same type, using a pointer. Here\'s some sample code that does not compile:
#include
Here's another approach; I've never had reason to do this, but it has the advantage of not mucking up the struct definition. Create a an array of pointers to the members you're interested in, and then iterate over that array:
typedef struct { int mem1; int mem2; int mem3, int mem4; } foo;
...
foo theStruct;
int *structMembers[4] = { &theStruct.mem1, &theStruct.mem2,
&theStruct.mem3, &theStruct.mem4};
...
for (i = 0; i < 4; i++)
{
printf("%d\n", *structMembers[i]);
}
This way you don't have to worry about alignment issues biting you, and you can arbitrarily order how you want to iterate over the members (e.g., you could order it so the walk is "mem4, mem3, mem2, mem1").