Menu
Recherche




Articles & Documents

Fermer Tutoriaux

Lettre d'information
Pour avoir des nouvelles de ce site, inscrivez-vous à notre Newsletter.
S'abonner
Se désabonner
22 Abonnés
CPDB.Net

Nouvelles des Amis
Visites

   visiteurs

   visiteurs en ligne

Webmaster - Infos
forum.gifForum - ppCompiler - Sujet n°21

Forum - Forum
ppCompiler - ppCompiler


actif  Sujet n° 21  Working with pointers in PPC

le 06/04/2010 @ 10:36
par vasalvit

Anonyme



Hi All.


I'm is proffessional in C/C++, but I newbie in PASCAL (PPC).  

I have some questions about using pointers.


1. Addressing arithmetic.

In C:

int* buffer = ...

* (buffer + 1) = 10;


2. Converting pointers to Integer and over

In C:

int *ptr = ...

int val = ...

val = (int) ptr;

ptr = (int*) val


3. NIL and procedure type

I save pointer to procedure in variable.  Can I test this variable:

TYPE Proc = PROCEDURE;

VAR Ptr: Proc;

Ptr = SomeProcedure;

IF(NIL = Ptr) // not compiled

IF(NIL = Ptr^) // not compiled

IF(NIL = @Ptr) // compiled, but always FALSE, because @Ptr - address of variable (or not)


ps.  Sorry for me english.

pps. Inserting code not working in my browser (Opera 10.5)

Ecrire à vasalvit   Poster une réponse  Haut
Réponse n° 1
--------
le 11/04/2010 @ 00:23
par Philippe

Anonyme

visiteur
1. There is no pointer arithmetic in Pascal. As the array type exists, this arithmetic is useless. Moreover, you can implement it by casting to integer, and perform usual arithmetic, but this practice is not in the spirit of Pascal language, which is stongly structured.
Declare buffer as an array of integer and juste write buffer[1]:=10;
2. Direct cast is not allowed in standard Pascal and is not implemented in PP. Use a "record case of" type, which is the Pascal equivalent of "union" in C :
var pori= record case integer of 0:(p:pointer); 1:(i:integer) end; // pointer or integer
begin
pori.p:=ptr;
i:=pori.i;
end;
This can be useful when translating a program from C, but shoud not occurs in a program designed in Pascal.
3. The procedure type is not a pointer type. It cannot have a nil value. You can perform a cast of a procedure type to a pointer type by using an union (record case of)

I hope this helps.

Philippe.
Ecrire à Philippe   Poster une réponse  Haut
actif sujet actif   clos sujet clos   Important! Important!   Nouveau Nouveau message   -   Rectifier Rectifier message   Clôturer Clôturer sujet   Remonter Remonter
[]
Catégories de discussion  Forum 




^ Haut ^