Menu
Search




Articles & Documents

Close Tutorials

Newsletter
To receive news about this website, consider subscribing to our Newsletter.
Subscribe
Unsubscribe
22 Subscribers
CPDB.Net

Friends News
Visits

   visitors

   visitors online

Webmaster - Infos
forum.gifForum - ppCompiler - Topic #21

Forum - Forum
ppCompiler - ppCompiler


active  Topic # 21  Working with pointers in PPC

06/04/2010 @ 10:36
by vasalvit

Anonymous



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)

Write to vasalvit   Post an answer  Top
Answer n° 1
--------
11/04/2010 @ 00:23
by Philippe

Anonymous

visitor
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.
Write to Philippe   Post an answer  Top
active topic active   closed topic closed   Sticky Sticky   New New message   -   Correct Correct message   Close Close topic   Make sticky Make sticky
[]
Forum Topic  Forum 




^ Top ^