Question C-code to C#

aw48

Active member
Joined
Feb 9, 2013
Messages
36
Location
Germany
Programming Experience
10+
hi,
I'm trying to port a small C-programm to C#
the only knowledge of C I got writing a programm that
created C/C++ code from pascal-code a long long time ago
Last summer that old machine which allowed me to run those
old borland programms died away so I can't debug some old
C-code anymore.

I've come about this line :
#define NUMBER(x) (sizeof(x) / sizeof(*x))
where NUMBER is used like this
NUMBER(fpgrouptab))
and fpgrouptab is
unsigned int fpgrouptab[] = { 0xd9e8, 0xd9f0, 0xd9f8 };

the first line I made
static object NUMBER( object x) { return(sizeof(x) / sizeof(*x)); }
the second
int[] fpgrouptab = new int[] { 0xd9e8, 0xd9f0, 0xd9f8 };

However I don't get the expression sizeof(x) / sizeof(*x)

I guess it's calculating some kind of count but I'm unable
to do this in C#

Can anyone help me with this problem
many thanks in advance
 
Last edited:
Yes, that macro just get the count of elements are in an array. That macro only works on declared C and C++ arrays. If does not work in C and C++ that are passed as parameters (because the arrays devolve into pointers and the size of the original array is unknown to the caller).

Anyway, every C# array has a Length property that tells you how many are in the array. I'm surprised that you don't know this. If you were learning C# in any kind of structured manner, arrays are introduced shortly after variables.

Modern PCs still let you write and debug C and C++ code. I think the issue is more that the C and C++ code you are talking about was written for Borland's non standard version of C and C++. You could still run DOSBox and have Borland run in the DOSBox.
 
hi,
thanks for your reply
of course I know array length and I would say that's sizeof(x)
but why divide by sizeof(*x)
 
sizeof() returns the number of bytes. *x refers to the first element of an array. So the size of the total array (in bytes) divided by the size of a single element (in bytes) will give you the number of elements.
 
Back
Top Bottom