Help with C->C# Port

  • Thread starter Thread starter farc
  • Start date Start date
F

farc

I am in the process of converting an old C application to C#. The
application is loaded with pointers (as most are, I guess). How would I best
represent the following structures in C#?

typedef char ** mytypedef;

typedef struct _mystructure {
struct _mystructure *left, *right;
} mystructure;

Thanks,
Karch
 
I am in the process of converting an old C application to C#. The
application is loaded with pointers (as most are, I guess). How would I best
represent the following structures in C#?

You are probably aware of this but in case you're not: the difference
between C and C# is much greater than just a syntax difference. C is a
procedural language while C# is a OO language. C# programs are therefore
organized and designed in a completely different way than C programs are
and porting form C to C# usually involves much more than just changing the
syntax. Instead of simply doing a litteral port, It's usually better to
understand what the program does and then design a C# program that does the
same thing but using an OO approach.
typedef char ** mytypedef;

In C:
- char is a character. In C#, you would use the Char structure to store a
character. Note that in C, char can actually be used to store any 1 byte
value while in C#, Char represents a unicode (2 bytes) character and only
that.
- *char is a pointer to character. This is usually used to point to a null
terminated character string. For this, you would use a string in C#
- **char is a pointer to a pointer of char. This can for instance be used
to pass a string as a parameter to a function that might return a pointer
to another string via the same parameter. In C#, you would either pass a
string as a ref parameter or simply return a new string as the return value
of the function.
- typedef char ** mytypedef; defines mytypedef as being a **char. Any
variable declared as being a mytypedef would therefore be a **char
variable. In other words, mytypedef really is an alias for **char. In C#,
there is no typedef. Instead, there is the much safer and more powerfull
concept of classes.
typedef struct _mystructure {
struct _mystructure *left, *right;
} mystructure;

There are struct in C# too. However, my knowledge of the C syntax shows its
limits here and i can't really say what this struct declaration actually
does.
 
Thanks for the reply. Yes, I'm quickly realizing that a rewrite is probably
the best approach. After looking more closely, I know that generics along
with .net system types will probably solve most of my issues.
 
Mehdi said:
There are struct in C# too. However, my knowledge of the C syntax shows its
limits here and i can't really say what this struct declaration actually
does.

My C is mighty rusty too, but I think that it defines the type
_mystructure as a structure containing two pointers to structures of the
same type, then declares mystructure as such a structure.

As structures in C# are value types, you can't reference them in that
way. Instead you would use a class.

The simple version of that class would be:

public class _myclass {
public _myclass left, right;
}

With a bit of object orientation it would be:

public class _myclass {

private _myclass left, right;

public _myclass(_myclass left, _myclass right) {
this.left = left;
this.right = right;
}

public _myclass Left {
get { return this.left; }
set { this.left = value; }
}

public _myclass Right {
get { return this.right; }
set { this.right = value; }
}

}
 
farc said:
typedef char ** mytypedef;

Most likely, mytypedef is a string[] -- i.e. an array of strings. Or
or a LinkedList<string> said:
typedef struct _mystructure {
struct _mystructure *left, *right;
} mystructure;

Most likely this is the backbone of a LinkedList<T>, for some T that
you haven't included.
 
Back
Top