C -> C# - working with pointers and arrays -- help appreciate!!

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

Bill Butler

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 don't think this is your actual code.
- B is never assigned
- LWt is assigned but never used
- You increment B rather than A

Perhaps actual code would be a good idea

Now why exactly do you want to do this in C# with pointers?
Simple array indexing would suffice.

Bill
***** 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:

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

I don't think this is your actual code.
    - B is never assigned
    - LWt is assigned but never used
    - You increment B rather than A

Perhaps actual code would be a good idea

Now why exactly do you want to do this in C# with pointers?
Simple array indexing would suffice.

    Bill






***** 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:

<snip>- Hide quoted text -

- Show quoted text -

sorry Bill, just typos. Apologies. I tried to simplifed the code but
introduced some types. Correct version is below:

**** BEGIN NEW TYPO LESS VERSION ****

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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}
 
B

Bill Butler

**** BEGIN NEW TYPO LESS VERSION ****

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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}

Well, I just played with 'unsafe' for the first time to come up with this,
but it seems to do the trick.

public unsafe void Run()
{
double[] SomeArray = new double[]{1,2,3,4,5,6,7,8,9,10};

int lower = 0;
int upper = 4;
fixed(double *start = &SomeArray[lower])
fixed(double *end = &SomeArray[upper])
{
double Total=0.0;
for (double *A = start ; A <= end; A++ )
{
Total+= *A;
}
Console.WriteLine("Total:{0}", Total);
}
}

Of course, this could be done without any unsafe constructs, simply by
indexing into the array.

public void Run2()
{
double[] SomeArray = new double[]{1,2,3,4,5,6,7,8,9,10};

int lower = 0;
int upper = 4;

double Total=0.0;
for (int i = lower ; i <= upper; i++ )
{
Total+= SomeArray;
}
Console.WriteLine("Total:{0}", Total);

}

Soooo...why did you need to do this with pointers??

Bill
 
D

David Anton

That's still not C++ code (the 'for' loop will not compile without 3
statements).
Also, no C++ in their right mind would declare A and B as pointers in this
case.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++


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 don't think this is your actual code.
- B is never assigned
- LWt is assigned but never used
- You increment B rather than A

Perhaps actual code would be a good idea

Now why exactly do you want to do this in C# with pointers?
Simple array indexing would suffice.

Bill






***** 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:

<snip>- Hide quoted text -

- Show quoted text -

sorry Bill, just typos. Apologies. I tried to simplifed the code but
introduced some types. Correct version is below:

**** BEGIN NEW TYPO LESS VERSION ****

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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}
 
B

Bill Butler

David Anton said:
That's still not C++ code (the 'for' loop will not compile without 3
statements).

You are mistaken.
Any/all of the parts can be omitted.

for(;;) is a common idiom for creating an infinite loop

Also, no C++ in their right mind would declare A and B as pointers in this
case.

Many C/C++ programmers (especially the old school gang) thrive on writing
obtuse code.



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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}
 
D

David Anton

Ack - I knew that - I didn't look hard enough. I thought it was missing a
semi-colon...
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++


Bill Butler said:
David Anton said:
That's still not C++ code (the 'for' loop will not compile without 3
statements).

You are mistaken.
Any/all of the parts can be omitted.

for(;;) is a common idiom for creating an infinite loop

Also, no C++ in their right mind would declare A and B as pointers in this
case.

Many C/C++ programmers (especially the old school gang) thrive on writing
obtuse code.



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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}
 
B

Ben Voigt [C++ MVP]

Bill Butler said:
You are mistaken.
Any/all of the parts can be omitted.

for(;;) is a common idiom for creating an infinite loop



Many C/C++ programmers (especially the old school gang) thrive on writing
obtuse code.

The only thing that looks obtuse here is trying to cram something that
doesn't look like a for loop into a for statement. A while loop would have
been much, much cleaner.

Well, that and trying to use the register keyword with a non-integral type.
But compiler have ignored the register keyword for years anyway.

I'd have no problem with using pointers as follows:

double * p = SomeArray + lower;
size_t remaining = upper - lower;
double total = 0;
/* loop invariant: total = sum of SomeArray [lower, upper - remaining) */
while (remaining--)
total += *(p++);


which translates quite nicely into C# as well, here's the version with
indices:

int i = lower;
uint remaining = upper - lower;
double total = 0;
/* loop invariant: total = sum of SomeArray [lower, upper - remaining) */
while (remaining-- != 0)
total += SomeArray[i++];

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

for (A = SomeArray[lower], B= SomeArray[upper] ; A <= B; )

{
Total+= *A++;
}
 

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

Top