Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help converting this snippet from c to c#

Tags:

c

c#

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!

like image 725
Vikyboss Avatar asked Jul 18 '11 21:07

Vikyboss


People also ask

What is the need for conversion of data type in C?

Since,conversion of datatype is implicit type as 'int' is a subset of 'longtype' hence no need to explicitly convert data from one type to another. Compiler will automatically do conversion.

What is a code snippet in C?

Snippet is a programming term for a small region of re-usable source code, machine code, or text. Ordinarily, these are formally defined operative units to incorporate into larger programming modules. Snippet management is a feature of some text editors, program source code editors, IDEs, and related software.

Which code editor is best for C?

CodeLite It is considered one of the best IDE for code refactoring and supports Windows and Mac operating systems. It also provides better support for compilers with built-in GCC, Clang, and Visual C++. It is a good option for testing and debugging in C++ because of its easy-to-use and lightweight features.

What is type conversion example?

In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.


2 Answers

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 ...

}
like image 138
SLaks Avatar answered Sep 27 '22 17:09

SLaks


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.

like image 37
Ed Bayiates Avatar answered Sep 27 '22 15:09

Ed Bayiates