How to iterate through a struct's fields?

arduino iterate through struct
c array of structs
create struct
array of struct in struct
c new struct
fill array of structs c
c access array of structs
struct function in c

I have a simple struct that looks like this:

public struct GridNeighbours
    public static Vector2Int North = new Vector2Int(0, 1);
    public static Vector2Int South = new Vector2Int(0, -1);
    public static Vector2Int East = new Vector2Int(1, 0);
    public static Vector2Int West = new Vector2Int(-1, 0);

Is there a way to "iterate" through each of these fields somehow with a for loop? Do structs by chance index their fields or something? It would make my code a lot cleaner if it was possible but I am not sure how to make it iterative.

Edit: due to this being used in a hot path in a game, is there a way to rewrite this so i can avoid reflection ?

This is not a duplicate since i need an alternative approach to avoid reflection.

public struct GridNeighbours {

    public enum Cardinal { North, East, South, West }

    public static Vector2Int[] Neighbours = { new Vector2Int(0, 1), new Vector2Int(1, 0), new Vector2Int(0, -1), new Vector2Int(-1, 0) };

    public Vector2Int this[Cardinal dirn] {
        get { return this[(int)dirn]; }
        set { this[(int)dirn] = value; }

    public Vector2Int this[int dirn] {
        get { return Neighbours[dirn]; }
        set { Neighbours[dirn] = value; }

Example use:

var cell = new GridNeighbours();
var NorthNeighbour = cell[Cardinal.North];
Assert(cell[Cardinal.South] == cell[2]); // This is true!


Alternatively, if you want "direct" properties:

public struct GridNeighbours {
    public enum Cardinal { North, East, South, West }

    public static Vector2Int[] Neighbours = { new Vector2Int(0, 1), new Vector2Int(1, 0), new Vector2Int(0, -1), new Vector2Int(-1, 0) };

    public static Vector2Int North { get { return Neighbours[0]; } set { Neighbours[0] = value; } }
    public static Vector2Int East { get { return Neighbours[1]; } set { Neighbours[1] = value; } }
    public static Vector2Int South { get { return Neighbours[2]; } set { Neighbours[2] = value; } }
    public static Vector2Int West { get { return Neighbours[3]; } set { Neighbours[3] = value; } }

How to iterate through all the elements of a single C struct, without , There are some clues through alignment and size of the structure but otherwise, it's unknown. in C, structs are not that different in creation from arrays. The only  void func1(struct example) {Iterate through the struct variables/elements}-----The above is what I am trying to do.. Could you please suggest something else if what I am trying is impossible in C++. @Flurite: I'm working with Structs (because if I use an array I can't have multiple data types) and my requirement is to have multiple datatypes.

Without using reflection I don't think you can do it given the signature you have there.

What you could do is to setup an indexer on the struct that would allow you to loop.

How to iterate through a struct ? - MATLAB Answers, How can i iterate through a struct in matlab ? Here is my code that does not work. im_filenames = dir('./JAAD_frames/video_0001/');. for name  Example: Looping Over Structs. This example shows how to loop over a Struct to perform actions on the various levels. The example calls two other entries: REMOVE_TAGS implements a simple loop that recursively removes the tags on a Struct and all its members and sub-members.

If you mean the possible values in the struct will not be known in the compile-time, then you can try using Reflection. More specifically, using Type.GetFields() or Type.GetProperties().


Type structType = typeof(GridNeighbours); 
FieldInfo[] fields = structType.GetFields(); 
Foreach(FieldInfo field in fields) 
    //Do something

But keeping the fact in mind that there will always be four directions, why a simple GridNeighboursInstance.North is not a preference for you.

Iterate through struct members, taken from , looping struct. Hello, Thanks in advance. I like to loop through the members of the following struct. How can I do that? Code: [View]. /* I will store  in c++11 you could use a std::tuple<int,int,char*> instead of your struct A, it will store the same kind of elements and is far easier to iterate over (using some template magic). the elements won't have names ranging from 'a' to 'c' but if you'd like to print it that way this of course can be accomplished by some extra lines of code.

If you don't want to use a dynamic way so I can suggest you to use a static way! When you have only those four members and you want to iterate over them you need to implement an AsEnumerable method inside your struct something like this:

public static IEnumerable<Vector2Int> AsEnumerable()
    for (var i = 0; i < 4; i++)
        switch (i)
            case 1:
                yield return North;
            case 2:
                yield return South;
            case 3:
                yield return East;
            case 4:
                yield return West;

I think there are some better way, But HTH ;).

looping struct, Iterating through Struct Members. Suppose I have a struct with 20 members. I want to assign each of those structs with values. Instead of  You can easily iterate through the members of a struct and print them using standard C++: A a; printf(a.a); printf(a.b); printf(a.c);. There are plenty of ways to make the syntax more like the desired 'loop', using some kind of custom reflection mechanism.

Iterating through Struct Members, You can iterate through an array of structs but not the struct members. Learn about Classes. You have a struct with function as well as data  If you want to Iterate through the Fields and Values of a struct then you can use the below Go code as a reference. Note: If the Fields in your struct are not exported then the v.Field(i).Interface() will give panic panic: reflect.Value.Interface: cannot return value obtained from unexported field or method.

Iterate through struct, The struct is dynamic and can have any number of members / elements of any data type. Is there a way to iterate though such kind of structs? Since fields or fns are cell arrays, you have to index with curly brackets {} in order to access the contents of the cell, i.e. the string. Note that instead of looping over a number, you can also loop over fields directly, making use of a neat Matlab features that lets you loop through any array.

Iterate through a struct - C++ Forum, in the struct and then iterate through them? I want to have a function that takes the struct as argument and then prompts the user for input for all  It may not be a good idea to do so, because the structure will vary according to how it was padded by the compiler and so on. If the intention is to serialise the structure to store it on disk or pass it to another process, you need to know what y

  • I think you are looking for this…
  • Hmm is there any way to re-write it so i don't have to use reflection? This is used in a game and reflection is a bit performant heavy im told. Which is no good for pathfinding etc.
  • You could use reflection initially to get the structs, but compile the getter so future calls are less expensive.
  • @CoderofCode see my edit before flagging duplicate >.>
  • Before diving into that, can you explain more about what you're trying to accomplish?
  • A static Dictionary<int,Vector2Int> would likely be quicker. Although with only 4 items I'm starting to wonder if the overhead of the indexing would nullify the efficiency.