Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm not familiar with Programming and I want to convert this code in c to c#..but I don't know what does this statement (wsave + n ) mean..
The code:
Void func(int n,double *r,double *wsave,int *ifac)
{
Func2(n, r, wsave, wsave + n, ifac);
}
Does this mean to add n to all elements in the array?
Can any one give me the equivalent code in c#?

What I have tried:

I'm trying to convert code above that written in c to c# code.
Posted
Updated 16-Jan-20 11:03am
v4
Comments
CPallini 17-Jan-20 3:36am    
You need a 'Nonsense' converter, in order to convert C nonsense to C# nonsense.

1 solution

Firstly, that won't compile: both C and C# are case sensitive, so Void is not the same as void - you want the latter, not the former.

Secondly, You don't want to convert that code to C#, not even slightly. That's using pointers, and pointers in C# are really, really hard to use - and that's deliberate. As a beginner, you should not be going anywhere near pointers in C#!

And without pointers, there is no direct equivelant of the C code in C# because what wsave + n does is return a pointer to a double n places up in the array or doubles that wsave refers to, and without pointers there is no direct way to do that: you cannot reference an array in C# from any point other than the start without copying it, and that doesn't do the same thing at all.

I'm sorry, but you will have to go back to the whole C code, work out exactly what it does in detail, and design a new C# app to do the same thing using C# data structures. translating it will not produce a "good C# program" - if it's even possible without using unsafe code.
 
Share this answer
 
Comments
Member 14718560 16-Jan-20 18:51pm    
Sir, if I replace this array with regular array (double[] wsave) can you tell me what would be the result of this statement (wsave+n) by considering n=10 and array size =3?
Does this true:
wsave[0]=3
wsave[1]=3
wsave[2]=3
OriginalGriff 17-Jan-20 3:28am    
A compilation error in C#.

Stop assuming that C# is a superset of C, or that "they are basically the same". They aren't, and particularly when you start with C pointers, you cannot just translate the code - it has to be redesigned to work at all, let alone well.

You can think of C# References as "pointers on steroids" but they aren't - they are a very different thing, and some of the thinks you can do with pointers just won't work in C# because if they did they could cause real problems. In C, wsave + n is legal, because C supports pointer arithmetic and returns a pointer to a "new array" which happens to be a portion of the original. You cannot do that with references in C# because if the original reference to the start of the array is discarded, the whole array can be released and destroyed at any time without warning - and then your new reference would be "hanging in midair" pointing at nowhere. This is analogous to the C problem of returning a pointer to a local variable in a function. Since local variables are created on the stack, they are deallocated when the function returns, so the pointer is referencing memory that is no longer assigned and which can be reused.

Stop thinking of them as the same language, and accept that translation is not going to work: you need to design a new app based on the functionality of the old, not on it's code base.
CPallini 17-Jan-20 3:37am    
5.

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