Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to convert my code from c with pointers into c#
code in C is like this 

myMethodName(Word16 exc[],Word16 T0)
{
  Word16 *p_exc;
  Word16 *a;
  a=&(exc[-T0])
  a++;
  b=a;
  c=a;
  a++;
  p_exc = exc;
for (i = 10; i > 0; i--)
{
	s2 += ((Word32) * (b--));
	s1 += ((Word32) * (b));
        s1 += ((Word32) * (c++));
        s2 += ((Word32) * (c));
}
*(p_exc++) = (Word16)(s1 >> 15);
}


What I have tried:

I Have tried this
myMethodName(Int16[] exc,Int16 T0)
{
 Int16 pIndex=0;
 Int16 a=0;
 a = T0;
a++;
b=a;
c=a;
for (i = 10; i > 0; i--)
{
	s2 += ((Int32) * (b--));
	s1 += ((Int32) * (b));
        s1 += ((Int32) * (c++));
        s2 += ((Int32) * (c));
}
p_exc[pIndex++] = (Int16)(s1 >> 15);
}
Posted
Updated 19-Nov-17 1:58am

That won't compile without the use of the unsafe keyword - it is not recommended to use pointers in C# without very good reason and the unsafe keyword (and build option) are there to enforce that.
And your C# code is significantly different from the C original to ensure that even with unsafe and the use of pointers, it won't do the same job without significant changes to the external code as well.

Generally speaking, converting from one language to another does not result in "good code" and starting from something using negative indexes if probably never togin to work well.
You would be much better served by using the C code as a guide, and working out why it needs such indexes. Then designing C# code that doesn't - you will get much more maintainable code as a result.
 
Share this answer
 
Comments
Member 11446383 19-Nov-17 8:28am    
Thanks for reply
Kindly explain with example,I do not understand this
OriginalGriff 19-Nov-17 8:32am    
What part do you not understand?
Quote:
Convert negative index in C into C#

Basically, negative indexes do not exist.
You need to understand the underlying structure which appears to have a negative index when in fact it is a positive one with another base address.

You need to analyze your app to understand what is what.
 
Share this answer
 
Comments
Member 11446383 19-Nov-17 8:28am    
Thanks for reply
Kindly explain with example,I do not understand this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900