5
typedef struct {
    int e1;
    int e2;
    int e3;
    int e4;
    int e5;
} abc;


void Hello(abc * a, int index)
{
    int * post = (&(a->e1) + index);

    int i;
    for(i = 0; i<5; i++)
    {
        *(post + i) = i;    
    }
}

The problem I face here is how they able to access the next element in the struct by

*(post + i)

I'm not sure how all these would be done in C# and moreover, I don't want to use unsafe pointers in C#, but something alternate to it.

Thanks!

3
  • 3
    The first thing to realize is that the code isn't safe (doesn't have defined results) in C. That said, I'd create an array to hold the data, and aliases of some sort (e.g., properties) to allow access via names. Commented Jul 18, 2011 at 21:27
  • 1
    Why are you trying to convert this code? What are you trying to achieve? Commented Jul 18, 2011 at 21:37
  • This is part of my work. We were working on porting parts of firmware to a gui application. And this is a concept where I got stuck at. Thanks Commented Jul 19, 2011 at 13:31

3 Answers 3

3

You should replace the struct with an array of 5 elements.

If you want to, you can wrap the array in a class with five properties.

edit...

When you say 'Wrap,' it generally means to write properties in a class that set or get the value of either a single variable, an array element, or a member of another class whose instance lives inside your class (the usual usage here = 'wrap an object'). Very useful for separating concerns and joining functionality of multiple objects. Technically, all simple properties just 'wrap' their private member variables.

Sample per comment:

class test
{

    int[] e = new int[5];
    public void Hello(int index)
    {
        for (int i = 0; i <= 4; i++) {
            // will always happen if index != 0
            if (i + index > 4) {
                MsgBox("Original code would have overwritten memory. .Net will now blow up.");
            }
            e[i + index] = i;
        }
    }

    public int e1 {
        get { return e[0]; }
        set { e[0] = value; }
    }

    public int e2 {
        get { return e[1]; }
        set { e[1] = value; }
    }

    //' ETC etc etc with e3-e5 ...

}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please explain more on that, I can't understand the meaning of wrap the array in a class.
2

The problem with the C code is that if index is greater than 0 it runs off the end of the abc struct, thus overwriting random memory. This is exactly why C#, a safer language, does not allow these sorts of things. The way I'd implement your code in C# would be:

struct abc
{
    public int[] e;
}

void Hello(ref abc a, int index)
{
    a.e = new int[5];
    for (int i = 0;  i < 5;  ++i)
        a.e[index + i] = i;
}

Note that if index > 0, you'll get an out of bounds exception instead of possibly silent memory overwriting as you would in the C snippet.

Comments

1

The thinking behind the C codes is an ill fit for C#. The C code is based on the assumption that the fields of the struct will be placed sequentially in memory in the order defined the fields are defined in. The above looks like either homework or a contrived example. Without knowing the real intent it's hard to give a concrete example in C#.

other examples here suggest changing the data structure but if you can't/don't want to do that, you can use reflection combined with an array of objects of the struct type to accomplish the same result as above.

void Hello(abc currentObj){
  var fields = typeof(abc).GetFields();
  for(var i = 0;i<fields.Length;i++){
    fields[i].SetValue(currentObj,i);
  }
}

1 Comment

This is part of my work. We were working on porting parts of firmware to a gui application. And this is a concept where I got stuck at. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.