Page 1 of 1
OH, Pascal !
Posted: Thu Oct 09, 2008 4:44 am
by i586coder
Hi folks,
I have trouble with this pascal chunk.
How I can convert this chunk to C code
Code: Select all
type
points:=array[1..10] of integer
procedure fill_area_solid(count: integer; x,y:points);
type
each_entry:=record
y_top:integer;
x_int:real;
delta_y:integer;
x_change_per_scan:real
end;
list=array[0..10] of each_entry;
var
slides:list
side_count,first_s,last_s,scan,bottomscan,x_int_count,r :integer;
begin
{any code whatever}
end.
note:
i copied this code as is from source,& my knowledge of pascal is so faaar
Re: OH, Pascal !
Posted: Thu Oct 09, 2008 6:45 am
by Velko
Translated almost literally:
Code: Select all
struct each_entry {
int y_top;
float x_int;
int delta_y;
float x_change_per_scan;
};
typedef int points[10];
typedef struct each_entry list[11];
void fill_area_solid(int count, points x, points y)
{
list slides;
int side_count,first_s,last_s,scan,bottomscan,x_int_count,r;
/* any code whatever */
}
Note about points: in Pascal it's 1-based array. In this C code 0-based, of course. And yes, arrays are passed by value
.
Re: OH, Pascal !
Posted: Thu Oct 09, 2008 8:18 am
by i586coder
Re: OH, Pascal !
Posted: Thu Oct 09, 2008 2:23 pm
by Velko
Well, of course that code needs a little optimization. Passing two 40-byte variables by value is not a very good idea. They should be passed by reference/pointer.
In Pascal that should be:
Code: Select all
procedure fill_area_solid(count: integer; const x,y:points);
In C:
Code: Select all
void fill_area_solid(int count, int *x, int *y)
Even better would be to use array of
struct point instead of 2 seperate arrays
Re: OH, Pascal !
Posted: Fri Oct 10, 2008 1:02 am
by pcmattman
And yes, arrays are passed by value
Huh? Structs are passed by value (unless explicitly passed by reference, which IIRC you can't do in C - so you pass as a pointer). Arrays in C are effectively pointers, and as such passing an array is passing an address.
Re: OH, Pascal !
Posted: Fri Oct 10, 2008 3:04 am
by Velko
With,
you get a type
points and sizeof(points)==40.
Then you declare a function
Code: Select all
void fill_area_solid(int count, points x, points y)
which expects parameters x and y to be that 40-byte chunk. Or am I wrong?
I'm not saying thats good, but thats exactly what Pascal code was doing.
Re: OH, Pascal !
Posted: Fri Oct 10, 2008 6:07 pm
by pcmattman
That may be what the Pascal code was doing. but I was talking about C in response to this:
In this C code 0-based, of course. And yes, arrays are passed by value