Board index » delphi » Why won't this code work?

Why won't this code work?

You wouldn't think it by this question, but I have been writing C and assembly
for 15 years, and Delphi for 4 or so. Luckily I have largely avoided pointers in
Delphi until now. My syntax below may not be exactly correct, but I made it up
on the fly.

Why won't this work?:

type MyType
??? blah
??? blah
end;

procedure??? MyProc;
var
??? Ptr?????? :Pointer;
begin
??? Ptr:=@AddressOfBuffer;
??? While (SomethingIsTrue) do
??? begin
??????? DoSomethingWith(Ptr);
??????? Ptr:=Ptr+SizeOf(MyType);??? //This is the problem
??? end;
end;

I guess my question is really; How do you add a number to a pointer? You can
Inc() the pointer, but can't add to it. I know I can make a type that is a
pointer to MyType and Inc() that, but that will not work for me in this case.
?

--
Thanks,
Ron Davis
c...@oaktree.net
?

 

Re:Why won't this code work?


Ron Davis <c...@Oaktree.net> skrev i
diskussionsgruppsmeddelandet:37C6D75A.C110...@Oaktree.net...

Quote
> You wouldn't think it by this question, but I have been writing C and
assembly
> for 15 years, and Delphi for 4 or so. Luckily I have largely avoided
pointers in
> Delphi until now. My syntax below may not be exactly correct, but I made
it up
> on the fly.

> Why won't this work?:

> type MyType
> blah
> blah
> end;

> procedure MyProc;
> var
> Ptr :Pointer;
> begin
> Ptr:=@AddressOfBuffer;
> While (SomethingIsTrue) do
> begin
> DoSomethingWith(Ptr);
> Ptr:=Ptr+SizeOf(MyType); file://This is the problem
> end;
> end;

> I guess my question is really; How do you add a number to a pointer? You
can
> Inc() the pointer, but can't add to it. I know I can make a type that is a
> pointer to MyType and Inc() that, but that will not work for me in this

case.

You could typecast the Pointer like this:
  Integer(Ptr) := Integer(Ptr) + SizeOf(MyType);

'I know I can make a type that is a pointer to MyType and Inc() that, but
that will not work for me in this case.'
What do you mean by that?

- Show quoted text -

Quote
> --
> Thanks,
> Ron Davis
> c...@oaktree.net

Re:Why won't this code work?


You can't increment or add to a pointer - well you can but it isn't a clever
idea.
You want to add to the value that the pointer is pointing to.

Still having looked at your code I think you really do want to add to the
pointer.
Try using inc with the optional parameter i.e. inc(ptr, SizeOf(MyType)).
Alternatively you could write an add function for pointers but I haven't a
clue where to start.

regards

Alistair

Quote
Ron Davis <c...@Oaktree.net> wrote in message

news:37C6D75A.C110BE1@Oaktree.net...
Quote
> You wouldn't think it by this question, but I have been writing C and
assembly
> for 15 years, and Delphi for 4 or so. Luckily I have largely avoided
pointers in
> Delphi until now. My syntax below may not be exactly correct, but I made
it up
> on the fly.

> Why won't this work?:

> type MyType
> blah
> blah
> end;

> procedure MyProc;
> var
> Ptr :Pointer;
> begin
> Ptr:=@AddressOfBuffer;
> While (SomethingIsTrue) do
> begin
> DoSomethingWith(Ptr);
> Ptr:=Ptr+SizeOf(MyType); file://This is the problem
> end;
> end;

> I guess my question is really; How do you add a number to a pointer? You
can
> Inc() the pointer, but can't add to it. I know I can make a type that is a
> pointer to MyType and Inc() that, but that will not work for me in this
case.

> --
> Thanks,
> Ron Davis
> c...@oaktree.net

Re:Why won't this code work?


You can do a couple of things,
 if it was a Typed pointer you could use the Inc(P) on because the compiler
would know the referrence destenation size , how ever, since you are not doing
so you can type case it..
Dword(Ptr) := Dword(Ptr)+Sizeof(What ever);
--
 a more elligent way to do this is...
type  MyArray = Array[0..0] of MyType;
Var
  MyVar:^Myarray;
begin
  MyVar := @Buffer;
  X:Integer;
Begin
  X := 0;
 If MyVar[X] = ????? Then Inc(X)  ect....
 End;
 --
Here you have created an Pointer To an array that the compiler will auto reference
by pointer using the X as the offset value..

 I think this is more of what your looking for..

Quote
Ron Davis wrote:
> You wouldn't think it by this question, but I have been writing C and assembly
> for 15 years, and Delphi for 4 or so. Luckily I have largely avoided pointers in
> Delphi until now. My syntax below may not be exactly correct, but I made it up
> on the fly.

> Why won't this work?:

> type MyType
>     blah
>     blah
> end;

> procedure    MyProc;
> var
>     Ptr       :Pointer;
> begin
>     Ptr:=@AddressOfBuffer;
>     While (SomethingIsTrue) do
>     begin
>         DoSomethingWith(Ptr);
>         Ptr:=Ptr+SizeOf(MyType);    //This is the problem
>     end;
> end;

> I guess my question is really; How do you add a number to a pointer? You can
> Inc() the pointer, but can't add to it. I know I can make a type that is a
> pointer to MyType and Inc() that, but that will not work for me in this case.

> --
> Thanks,
> Ron Davis
> c...@oaktree.net

Other Threads