Pascal --> C question

Dave Jones djones at megatest.UUCP
Mon Mar 7 16:36:44 AEST 1988


in article <650001 at hpcilzb.HP.COM>, tedj at hpcilzb.HP.COM (Ted Johnson) says:
> 
> 
> 
> 
> 
> 
> 
> I am contemplating translating a Pascal program to C,
> but am not sure if it can be done very easily....  
> 
> Is there a C equivalent for the Pascal declaration:
> 
> 	SinWave: packed array[0..255] of char;
> 
> ???
> 
> Is the C equivalent simply:
> 
> 	char SinWave[256];
> 
> ???
> 
> Any help/pointers are appreciated!
> 
> -Ted

What do you mean by 'C equivalent'?  If you intend to link your translated
program with object modules compiled for Pascal linkage, then you have to know
how the implementers of your C and your Pascal made some arbitrary 
choices, among them how to represent arrays of characters.  (C implementations,
almost always, conventionally terminate strings with null characters.
Pascal strings have a compile-time-defined fixed length. Sometimes the
implementations prefix the Pascal string with the integer value of the length
of the string.)  I would not recommend that such a project be undertaken
by a non-guru.  There might also be some problem with reading files of 
records generated by a program written in another language, but probably not.

If you only want to translate a stand-alone Pascal program into C, switching 
from the Pascal runtime library to the C runtime library (if they are 
different on your machine), then you are free to define your own mapping, and
it should be pretty smooth sailing.  In that case, the mapping of the
Pascal packed array into the C char-array is perfectly reasonable.

The only tricky problem you may come across is in translating Pascal
procedures which have "nested scope".  If you have none of these, congratulate
yourself for having avoided a dubious feature.  If you have used such
nestings, I would recommend that you remove them.  If you don't want to
do that, you will need to implement a 'display', and resolve references
to vars in the parent's scopes occordingly.  Any good book on languages
or compiler construction will have a section on displays.

By the way, do you know about C++?  If you are going to undertake a translation
from Pascal, I would suggest that your target be C++ rather than C.


	Good luck,

	Dave J.



More information about the Comp.lang.c mailing list