Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-07

Translate C to Pascal

16 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vinzent Höfler
to Gene Buckle: er... Closest would be: type record = foo bar : integer; end; var a : foo; I _think_. g.
Show quoted text 21 lines
Datum: Wed, 30 Jul 2008 10:10:09 -0700 (PDT) Von: Gene Buckle An: FPC-Pascal users discussions Betreff: Re: [fpc-pascal] Translate C to Pascal > How to translate this: > > struct a; > > er... Closest would be: type record = foo bar : integer; end; var a : foo;

No. The a is not an instantiation, it's an incomplete type. I wouldn't translate it at all, it's merely a forward declaration. As the (C-)compiler wouldn't know the size of the type at this point, it can't instantiate anything (although pointer to it would be possible, AFAIR).

Vinzent.

GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion! http://games.entertainment.gmx.net/de/entertainment/games/free/puzzle/6169196

Reply by email

Lourival Mendes
to Jilani Khaldi

I would do something like this:

C
----------------------------
struct product {
  int weight;
  float price;
} ;
product apple;
-----------------------------
Pascal
----------------------------
Type
   product = record
      weight: Integer ;
      price: double;
    end;
Var
  apple: product ;
----------------------------

This is the reason that I do believe that is missing something on the code:

struct a;

Best Regards

Lourival Mendes

2008/7/30 Jilani Khaldi :

Show quoted text 15 lines
Gene Buckle wrote: >> >> How to translate this: >> >> struct a; a: pointer; -- Jilani KHALDI --------------------- http://www.jkhaldi.eu _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Damien Gerard
to Lourival Mendes
Lourival Mendes wrote 29 lines
I would do something like this: C ---------------------------- struct product { int weight; float price; } ; product apple; ----------------------------- Pascal ---------------------------- Type product = record weight: Integer ; price: double; end; Var apple: product ; ---------------------------- This is the reason that I do believe that is missing something on the code: struct a;

A C-struct is merely a record. A struct with methods is a class.

struct Foo
{
Foo() : tag(0) {}

     int tag;
     void compute() {...}

};

would be

class Foo
private
   FTag: Integer;
public
   constructor Create;
   procedure compute;
   property tag: Integer read FTag write FTag;
end;

the property is not really needed of course.

Damien Gerard Si ces robots s'humanisaient, inversement les êtres humains se robotiseraient-ils ?

Reply by email

Jilani Khaldi
to Marc Weustink
Marc Weustink wrote 14 lines
Micha Nelissen wrote: > Mattias Gärtner wrote: >> How to translate this: >> >> struct a; > > Isn't this a forward declaration? So sometime later it needs to declare > 'struct a { ... };' ? If not, can't it be translated as: a: record end; ?

Yes, it can.

Jilani KHALDI --------------------- http://www.jkhaldi.eu

Reply by email

Mattias Gaertner
to Jilani Khaldi
Show quoted text 16 lines
Marc Weustink wrote: > Micha Nelissen wrote: >> Mattias Gärtner wrote: >>> How to translate this: >>> >>> struct a; >> >> Isn't this a forward declaration? So sometime later it needs to >> declare 'struct a { ... };' ? > > If not, can't it be translated as: > > a: record end; > > ? Yes, it can.

Thanks. I will use that.

Mattias

Reply by email

Felipe Monteiro de Carvalho
to Vinzent Höfler: What for? The C statement is empty, it's not a variable and not even a type. So before…
Vinzent Höfler wrote
What for? The C statement is empty, it's not a variable and not even a type. So before translating that into an empty Pascal-record, you should rather look at what the actually used structur in the C-code is.

It is usually used for having an opaque type, referencing it with a pointer. I would put in pascal both the empty structure and a pointer type to it.

  a: record end;
  pa: ^a;
Felipe Monteiro de Carvalho

Reply by email

Vinzent Höfler
to Mattias Gaertner: On Tue, 12 Aug 2008 09:39:36 +0200 Thanks. I will use that. Mattias
Mattias Gaertner wrote 19 lines
On Tue, 12 Aug 2008 09:39:36 +0200 Jilani Khaldi wrote: > Marc Weustink wrote: >> Micha Nelissen wrote: >>> Mattias Gärtner wrote: >>>> How to translate this: >>>> >>>> struct a; >>> Isn't this a forward declaration? So sometime later it needs to >>> declare 'struct a { ... };' ? >> If not, can't it be translated as: >> >> a: record end; >> >> ? > Yes, it can. Thanks. I will use that.

What for? The C statement is empty, it's not a variable and not even a type. So before translating that into an empty Pascal-record, you should rather look at what the actually used structur in the C-code is.

Vinzent.

Reply by email

Vinzent Höfler
to Felipe Monteiro de Carvalho: On Wed, Aug 13, 2008 at 7:38 AM, Vinzent Höfler It is usually used for having an opaque…
Felipe Monteiro de Carvalho wrote 9 lines
On Wed, Aug 13, 2008 at 7:38 AM, Vinzent Höfler wrote: > What for? The C statement is empty, it's not a variable and not even a type. > So before translating that into an empty Pascal-record, you should rather > look at what the actually used structur in the C-code is. It is usually used for having an opaque type, referencing it with a pointer. I would put in pascal both the empty structure and a pointer type to it.

But then the structures won't match, opaque or not.

Mattias provided no context to his original question, so I don't know if it would impose a problem, but usually I leave those things out until I really need them. The type as is has no storage size associated to it, so it can't even be used that way in the C-code. Sure, you can have a pointer to it, but unless you cast the hell out of it, it still won't do any good.

Vinzent.

Reply by email

Mattias Gärtner
to Vinzent Höfler: What for? The C statement is empty, it's not a variable and not even a type. So before…

Zitat von Vinzent Höfler :

Show quoted text 8 lines
[...] >>> a: record end; > > Thanks. I will use that. What for? The C statement is empty, it's not a variable and not even a type. So before translating that into an empty Pascal-record, you should rather look at what the actually used structur in the C-code is.

Actually the C-Code does what C can do really good: obfuscating.

If I understand the code correct, the 'struct a;' itself is never used directly. It always uses 'struct a* foo'. So foo is a pointer to an empty struct a, which probably is the C equivalent of a typed pointer. To get strong type checking, I guess, it is ok to follow Felipe's advice:

type a = record end; pa=^a;

Mattias

Reply by email

Vinzent Höfler
to Mattias Gärtner
Mattias Gärtner wrote 10 lines
Zitat von Vinzent Höfler : > [...] >>>> a: record end; >> Thanks. I will use that. > What for? The C statement is empty, it's not a variable and not even a > type. So before translating that into an empty Pascal-record, you should > rather look at what the actually used structur in the C-code is. Actually the C-Code does what C can do really good: obfuscating.

Yeah.

Show quoted text 6 lines
If I understand the code correct, the 'struct a;' itself is never used directly. It always uses 'struct a* foo'. So foo is a pointer to an empty struct a, which probably is the C equivalent of a typed pointer. To get strong type checking, I guess, it is ok to follow Felipe's advice: type a = record end; pa=^a;

Well, I guess so, but it still doesn't make a lot of sense.

That "struct a" is not empty (storage size = 0), it is even non-existant (storage size = unknown). So the C-code may shuffle around the pointers, but unless this "struct a" (the "a" not even being a type here) is instantiated somewhere (internally in some library code?) such pointers are merely typed "void*".

Vinzent.

Reply by email

Mattias Gärtner
to Vinzent Höfler

Zitat von Vinzent Höfler :

Show quoted text 30 lines
Mattias Gärtner wrote: > Zitat von Vinzent Höfler : > >> [...] >>>>> a: record end; >>> Thanks. I will use that. >> What for? The C statement is empty, it's not a variable and not even a >> type. So before translating that into an empty Pascal-record, you should >> rather look at what the actually used structur in the C-code is. > > Actually the C-Code does what C can do really good: obfuscating. Yeah. > If I understand the code correct, the 'struct a;' itself is never used directly. > It always uses 'struct a* foo'. So foo is a pointer to an empty struct > a, which probably is the C equivalent of a typed pointer. To get strong type > checking, I guess, it is ok to follow Felipe's advice: > > type a = record end; pa=^a; Well, I guess so, but it still doesn't make a lot of sense. That "struct a" is not empty (storage size = 0), it is even non-existant (storage size = unknown). So the C-code may shuffle around the pointers, but unless this "struct a" (the "a" not even being a type here) is instantiated somewhere (internally in some library code?) such pointers are merely typed "void*".

The c compiler does not need the internals, so should the pascal bindings.

Mattias

Reply by email