OH, Pascal !

Programming, for all ages and all languages.
Post Reply
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

OH, Pascal !

Post 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 :oops:
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: OH, Pascal !

Post 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 :shock:.
If something looks overcomplicated, most likely it is.
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: OH, Pascal !

Post by i586coder »

=D> =D> =D> =D>
Thanks, it seems work
=D> =D> =D> =D>
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: OH, Pascal !

Post 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 :wink:
If something looks overcomplicated, most likely it is.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: OH, Pascal !

Post 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.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: OH, Pascal !

Post by Velko »

With,

Code: Select all

typedef int points[10];
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.
If something looks overcomplicated, most likely it is.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: OH, Pascal !

Post 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
Post Reply