Reputation:
I have the next code in C++:
for (long i=0; i < num_iter ; i++)
{
bp->bpgt(data[i%8], &data[i%8][3]);
if( bp->mse(&data[i%8][3]) < thresh)
break;
}
where bpgt is a procedure, and mse is a function, thresh is a Double type, data is a bi-dimensional matrix of Double types.
void bpgt(double *in,double *tgt);
double mse(double *tgt);
double data[][4]={
0,0,0,0,
0,0,1,1,
1,1,1,1 };
I've tried to pass it to Delphi code:
for i := 0 to FNum_Iter - 1 do begin
FBPN.bpgt(FData[i mod 8], ^FData[i mod 8,3]);
if FBPN.mse(@FData[i mod 8, 3]) < FThresh then
Break;
end;
but I've failed, because I'm a newbie in C++ and I dont know to translate the "&" operator. May Someone help me?
Thanks in advance.
Upvotes: 1
Views: 1480
Reputation: 13475
Over at Rudy's Delphi Corner, he has an excellent article about the pitfalls of converting C/C++ to Delphi. In my opinion, this is essential information when attempting this task. Here is the description:
This article is meant for everyone who needs to translate C/C++ headers to Delphi. I want to share some of the pitfalls you can encounter when converting from C or C++. This article is not a tutorial, just a discussion of frequently encountered problem cases. It is meant for the beginner as well as for the more experienced translator of C and C++.
He also wrote a "Conversion Helper Package" that installs into the Delphi IDE which aids in converting C/C++ code to Delphi:
(source: rvelthuis.de)
His other relevant articles on this topic include:
Upvotes: 0
Reputation: 84550
This is based on your reply to Fvu's answer. If they're arrays, that can complicate things, since C doesn't have real arrays, just pointers sprinkled with a light coating of powdered syntactic sugar. The solution depends on whether you're translating this completely into Delphi, or trying to write a Delphi routine that will work with C code.
If you're doing pure Delphi, declare an array type, like so
type
TDoubleArray = array[0..length] of double; //or "array of double" for a dynamic array
Then declare the parameter of the function as var Data: TDoubleArray
. If this is a dynamic array, have your for loop go from 0 to high(Data);
If this needs to work with C:
Because C doesn't have real arrays, you need to do some extra work. Declare your type like this:
type
TCDoubleArray = array[0..65535] of double;
PCDoubleArray = ^TCDoubleArray;
Any sufficiently large value will work for the upper bound of the array. We're not going to use all of it anyway. It just has to be bigger than you'll ever use. Make the parameter type a PCDoubleArray, which corresponds to double *
. You'll need to pass an extra parameter, which tells you where the end point of the array is. Have your for loop go from 0 to the end point, and access it by saying something like FData^[i mod 8]
.
Upvotes: 2
Reputation: 23289
You are close, try this:
for i := 0 to FNum_Iter - 1 do begin
FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);
if FBPN.mse(FData[i mod 8, 3]) < FThresh then
Break;
end;
Sorry, but I don't have delphi here to compile and try it by myself.
Upvotes: 0
Reputation: 32953
I would translate
void bpgt(double *in,double *tgt);
as
procedure bpgt(var in:double; var tgt: double)
Well, something like that, my Delphi is a bit rusty....
That way, in bpgt, you can change the value of tgt (and in).
Your call of bpgt will be
FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);
In fact Delphi's var (call by reference) is quite often usable as an exact functional equivalent of C/C++'s passing of pointers.
Upvotes: 2
Reputation: 27811
The & allows you to send the address of the variable into the function, rather than it's value. This call by reference allows the function to alter the value of the variable. I don't know the equivalent Delphi mechanism, but you want to pass the reference of your variable there, or change the procedure to a function, return the value and store it in the original variable.
Upvotes: 0