1
enum
{
    ARRAY1,
    ARRAY2,
    ARRAY3,
    ARRAY4,
    ARRAY5,
    STMEMBERCOUNT
};

STMEMBERCOUNT shows that the member list in to the structure

typedef struct {
   char array1[26];
   char array2[31];
   char array3[31];
   char array4[11];
   char array5[11];
}ST_ARRAY;


main()
{
    int i;
    int sizeofmember[STMEMBERCOUNT];

    for(i = 0; i < STMEMBERCOUNT; i++)
    {
        sizeofmember[i] = ???????
    }
}

so that my array can be filled as

sizeofmember[0] = 26
sizeofmember[1] = 31
sizeofmember[2] = 31
sizeofmember[3] = 11
sizeofmember[4] = 11
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
uVGo
  • 13
  • 3
  • 1
    Despite several question marks... I don't see a question here. – nhgrif Dec 07 '13 at 22:04
  • are you trying to iterate over the members of an `enum` ? Anyway I suggest to change your first part of the `enum` definition in `ARRAY1 = 0,` – user2485710 Dec 07 '13 at 22:07
  • sizeof() will only work if i put all Question marks like example: – uVGo Dec 07 '13 at 22:07
  • what is the point of `ST_ARRAY` anyway ? What kind of role is playing in your business logic ? – user2485710 Dec 07 '13 at 22:08
  • @JonahNelson: man can you provide an example that fill sizeofmember variable with the sizeof all member in structure using for loop – uVGo Dec 07 '13 at 22:10
  • Enumeration of fields at runtime is a feature of reflection. Neither C nor C++ has builtin support for that. http://stackoverflow.com/q/359237/1175253 , http://stackoverflow.com/q/41453/1175253 . You could use a C++11 tuple and a static (compile time) iteration. – Sam Dec 07 '13 at 22:11
  • sizeofmember[0] = 26 // sizeof(ST_ARRAY.array1) sizeofmember[1] = 31 // sizeof(ST_ARRAY.array2) sizeofmember[2] = 31 // sizeof(ST_ARRAY.array3) sizeofmember[3] = 11 // sizeof(ST_ARRAY.array4) sizeofmember[4] = 11 // sizeof(ST_ARRAY.array5) – uVGo Dec 07 '13 at 22:15
  • @JonahNelson i want all individual ST_ARRAY structure member size by loop iteration and fill it to sizeofmember variable, or is there any way to jump between the members of the structure by use of pointer – uVGo Dec 07 '13 at 22:26
  • @user2485710 i am posting first time so forget the line just want is individual ST_ARRAY structure member size by loop iteration and fill it to sizeofmember variable – uVGo Dec 07 '13 at 22:37

1 Answers1

0

If I understand correctly, you need to iterate over members of a struct or enum.

This is not possible in C/C++ mainly because a C program is compiled. The languages that allow to iterate over members usually have the programs interpreted or run in a virtual machine or in a runtime environment, where you can more easily have runtime information about your structures.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • i want to iterate on structure members only enum is just to find the number of iteration – uVGo Dec 07 '13 at 22:19