A little bit of C -> C# help required please!!

A

almurph

Hi everyone,

Hope I'm in the right forum. Apologies if I'm not but I have a
hybrid problem here between classic C and C#.
Is it possible to write the equivalent C code (that uses pointers and
arrays) in C#...


***** BEGIN DIFFICULT C STUFF ******


register double Total=0.0, *A, *B;
int lower;
int upper;


for (A = SomeArray[lower], LWt = SomeArray[upper] ; A <= B; )
{
Total+= *B++;
}


return Sum;

***** END DIFFICULT C STUFF *****


I'm not too worried by the "register" keyword. It's the
pointers and
array combo that C# has problems with. I am trying stuff like:


*** BEGIN SOME C# BUGGY EFFORT ***


unsafe
{
fixed (double* A = &SomeArray[lower])
{
fixed (double* B = &SomeArray[Upper])
{
for (A, B; A <= B;)
{
//Error here, it says "Only
assignment, call, increment,
decrement, and new object expressions //can
be used as a statement
}
}


}



}


*** END SOME C# BUGGY EFFORT ***

I would appreciate if anyone could offer any comments/
suggestions/
code-samples/help/ideas - would greatly appreciate any assistance in
this regard.


Thanks & cheers,
Al.
 
J

Jens Thoms Toerring

Hope I'm in the right forum. Apologies if I'm not but I have a
hybrid problem here between classic C and C#.
Is it possible to write the equivalent C code (that uses pointers and
arrays) in C#...

It's probably not too good an idea to ask in this group since
its topic is C and not C# - a group for C# is probably a much
better place since your main concern seems to be with C# and
not C.

But there already several issues with the C code you want to
translate and I am going to concentrate on that part.
***** BEGIN DIFFICULT C STUFF ******
register double Total=0.0, *A, *B;
int lower;
int upper;
for (A = SomeArray[lower], LWt = SomeArray[upper] ; A <= B; )
{
Total+= *B++;
}

What's 'SomeArray'? Is it really an array of double pointers
as the assignments to 'A' and 'LWt' (which is a variable that
isn't defined anywhere) suggests? Or is it an array of doubles?
In that case you would need

A = SomeArray + lower;

or maybe

A = &SomeArray[ lower ];

and

LWt = SomeArray + upper;

And what is 'LWt' good for? It's never used for anything.
And whre 'B' gets initialized?

It looks a bit as if you intended to write

for ( A = B = SomeArray + lower, LWt = SomeArray + upper; B <= LWt; )
Total += *B++;

in order to sum all elements of SomeArray between (and including)
the element at 'lower' and 'upper' - but that's pure guesswork.

But if that's the case and you have problems using pointers in
C++ then a different way to write the loop, not using pointers,
would be

int i;
double Total = 0.0;

for ( i = lower; i <= upper; i++ )
Total += SomeArray[ i ];

I guess that should be easy to translate in any language that has
arrays and for-like control structures. Of course, that won't do
the trick if 'SomeArray' is an array of pointers to doubles in-
stead...
return Sum;

Shouldn't you be returning 'Total'?
I'm not too worried by the "register" keyword.

You shouldn't, simply forget about it. It's just a hint to the
compiler that most modern compilers will disregard.

Regards, Jens
 
H

Harold Aptroot

Hi everyone,

Hope I'm in the right forum. Apologies if I'm not but I have a
hybrid problem here between classic C and C#.
Is it possible to write the equivalent C code (that uses pointers and
arrays) in C#...

[...]

I would appreciate if anyone could offer any comments/
suggestions/
code-samples/help/ideas - would greatly appreciate any assistance in
this regard.


Thanks & cheers,
Al.

Just keep in mind What it's doing, rather than How.
What it's doing: some dead code and then return a variable that wasn't
defined. Ok.
But I can guess what it Should be doing.

double Total=0.0;

/*you'd better initialise those otherwise it won't make sense*/
int lower;
int upper;

for (int i = lower; i <= upper; i++)
{ Total += SomeArray; }

return Total;

No pointers were needed since the pointers were just used to index the
array, at least as far as I could see.

good luck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top