Board index » delphi » Please help......about "Pointer"?

Please help......about "Pointer"?

hi......I have some questions about  "Pointer".....Can you help me?
Thanks......
I write an uncomplete program for example...

var
  InBuffer:pointer;
  p:Pchar;
begin
  if ReadFile( Handle, InBuffer^, Len , nRead, nil ) then
   begin
    P:=InBuffer ;
    Result:=Result+P^ ;
    Inc(P) ;
  end;
end;

Question:
1. InBuffer^ , ^InBuffer,InBuffer what are their difference?
2. Since " var InBuffer : pointer"..why the "^" add to the
"InBuffer"===>"InBuffer^"  ?
3. does variate must to declare " pointer",and add "^" to operator?
4. "P"  is "Pchar",what is the meaning of "P^"?
5. Result:=Result+P^ ;....what is the variate type of "Result".....how to
declare it?

 

Re:Please help......about "Pointer"?


Quote
"Alvin" <alvin_...@msn.com> wrote in message

news:9tfmuc$203$1@news.seed.net.tw...

Quote
> hi......I have some questions about  "Pointer".....Can you help me?
> Thanks......
> I write an uncomplete program for example...

> var
>   InBuffer:pointer;
>   p:Pchar;
> begin
>   if ReadFile( Handle, InBuffer^, Len , nRead, nil ) then
>    begin
>     P:=InBuffer ;
>     Result:=Result+P^ ;
>     Inc(P) ;
>   end;
> end;

> Question:
> 1. InBuffer^ , ^InBuffer,InBuffer what are their difference?

The ^ means "Points To", as in:
  InBuffer is a pointer.
  InBuffer^ is what InBuffer Points To.
  ^tInBuffer is something that Points To tInBuffer.

In the last of these, tInBuffer and ^tInBuffer are types. tInBuffer
could be an array, then ^tInBuffer is a pointer that points to that
sort of array. You could declare
  Var
    InBuffer: ^tInBuffer;
which says "InBuffer is a pointer variable that Points To an instance
of tInBuffer". But it's more useful to declare another type:
  Type
    tpInBuffer: ^tInBuffer;
  Var
    InBuffer: tpInBuffer;

If you use the InBuffer:^tInBuffer form, then InBuffer is implicitly
declared to be a type that points to InBuffer, but different from any
other type that points to InBuffer. If you explicitly give a name to a
type, then you can use that name in many declarations, to declare
variables of that type.

For example:
type
  tbuf = Array[1..10] of Byte;

  tpBuf = ^tbuf;

var
  p1 : ^tbuf;
  p2, p3 : ^tbuf;
  p4 : tpBuf;
  p5 : tpBuf;

Begin
  p2 := p3; // ok
  p1 := p2; // compiler error
  p5 := p4; // ok
  p5 := p1; // compiler error
end.

p1, p2 and p5 are separate types, although internally they both point
to the tBuf array type. p2 and p3 are the same type, because both are
created in the statement which created their type. p4 and p5 are the
same type, because you told the compiler that they were the same
type - and of course, to tell the compiler, you had to give it a name.

IMHO this is because Pascal tries to keep the "meaning" of types
separate from their internal representation. One type which happens to
point to an array of bytes will be the same as another, internally,
but they may have different meanings to the programmer. Suppose p1
points to a buffer for data coming from some data capture card, and p2
points to bytes going to the serial port. Suppose next year's model of
data capture card presents integers rather than bytes, so you will
want p1 to point to an array of Integer rather than byte. If the
compiler has allowed you to swap p1 with p2 willy-nilly, your program
would have errors. By forcing you to give it a name, you are prompted
to group the pointers by function. But only prompted. You could
declare all the variables to be tpBuf. But hopefully if the buffers
actually were different, you would use different types for them.

Quote
> 2. Since " var InBuffer : pointer"..why the "^" add to the
> "InBuffer"===>"InBuffer^"  ?

InBuffer is a pointer. InBuffer^ is what InBuffer Points To. Actually,
"Pointer" isn't a great type to use here, because you don't know what
type of datum InBuffer points to.

"Pointer" is a pointer type, just as tpBuf was in my example above. So
if InBuffer is of type Pointer, then InBuffer^ is what the pointer
points to. Buf whereas tbBuf points to an array of 10 bytes, Pointer
can point to anything. It's a way of getting around type-checking.

Quote
> 3. does variate must to declare " pointer",and add "^" to operator?

I'm sorry, I don't know how to parse that question.

Quote
> 4. "P"  is "Pchar",what is the meaning of "P^"?

PChar is a yukky sort of "pointer pretending to be a string" because C
does it that way. It was introduced in TurboPascal 6, or maybe 7.

In C, pointers and arrays are very closely related. int x[10];
declares an array of 10 integers, and the value of x is the address of
the first element. int *p; declares a pointer to integer, p = x;
points p at the array - so x is like a pointer to an array, and so is
p. And you can do p[5] = '!'; so p is like an array.

The PChar type was introduced to mimic this sort of weirdness. P is
either a pointer to a char or an array of chars. imho it was not one
of Borland's better innovations. But it's not going to go away.
Procedural variables and Class variables follow the same pattern as
PChar, in that you do not use ^ so we just have to live with it.

Quote
> 5. Result:=Result+P^ ;....what is the variate type of
"Result".....how to
> declare it?

As you said, your "program" above was incomplete. Result is a variable
that exists inside a function. It has the same type as the function
return value. Because it _is_ the function's return value. Assigning a
value to it defines what the function will return. So Result does not
make sense unless you have a function header above what you quoted.

Result is an alternative to the older Pascal way of using the function
name to define the return value. So

Function x:Integer;
Begin
  x := 1;
end;

is the same as

Function x:Integer;
Begin
  Result := 1;
end;

But with Result, you can read the value as well as assign a value to
it. For example,

Function y:String;
Begin
  y := 'this';
  y := y+' and that'; // WRONG!
end;

would NOT work, the second line would call y recursively. But

Function y:String;
Begin
  y := 'this'; // or Result := 'this';
  Result := Result+' and that';
end;

is fine.

And you don't declare Result. If you're in a function, Result is there
automatically.

p^ is a char. So if you're adding it to Result, presumably we're in a
function which returns a string. So Result is a string.

FP

Re:Please help......about "Pointer"?


In article <9tg54o$aa...@kermit.esat.net>, "Frank Peelo" <fpe...@eircom.net>
writes:

Quote
>  Type
>    tpInBuffer: ^tInBuffer;
>  Var
>    InBuffer: tpInBuffer;

I think it is better to follow the Delphi convention of a "T" prefix for a type
and a "P" prefix for a pointer, so you would code ...

type
  PBuffer = ^TBuffer;
  TBuffer = array[0..99] of byte;

Then I find less likelihood of error to use "Ptr" as a prefix to a pointer
variable ...

var
  PtrBuffer : PBuffer;

AFAIR declaring a type of "pointer to a type" is the only use of a caret prefix
in Delphi ("@" is used as an "address of" operator). And surprisingly one can
declare the "pointer to the type" before declaring the type.

Alan Lloyd
alangll...@aol.com

Re:Please help......about "Pointer"?


Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20011121153805.11899.00002186@nso-fd.aol.com...
Quote
> In article <9tg54o$aa...@kermit.esat.net>, "Frank Peelo"

<fpe...@eircom.net>

Quote
> writes:

> >  Type
> >    tpInBuffer: ^tInBuffer;
> >  Var
> >    InBuffer: tpInBuffer;

> I think it is better to follow the Delphi convention of a "T" prefix
for a type
> and a "P" prefix for a pointer, so you would code ...

> type
>   PBuffer = ^TBuffer;
>   TBuffer = array[0..99] of byte;

Whatever you're used to. Wasn't too happy with InBuffer as the name of
a pointer variable either, but I thought changing it would be just
confusing as the name came from the post I was replying to. It's not a
big issue in examples, as long as the type declarations are there. The
type of InBuffer^ was not mentioned in the original post so I could
put a 't' in the tInBuffer type. I've been using 'p' as a prefix in
pointer variables since before Turbo Vision, and I haven't used
explicit pointers too much in Delphi, so I'm afraid pBuffer looks like
a pointer to me, rather than a type. Hence tpInBuffer for the type of
InBuffer (which I would have called pInBuffer). As long as the type
declarations are there in the examples, I don't see the problem.

Quote
> Then I find less likelihood of error to use "Ptr" as a prefix to a
pointer
> variable ...

> var
>   PtrBuffer : PBuffer;

> AFAIR declaring a type of "pointer to a type" is the only use of a
caret prefix
> in Delphi ("@" is used as an "address of" operator). And

surprisingly one can

Quote
> declare the "pointer to the type" before declaring the type.

Not that surprising. Doing linked lists would be difficult without it.
The nodes in the list need a pointer to the next node, which is their
own type, which is not (fully) defined at that point:

Type
  tpNode = ^tNode;
  tNode = Record
    pNext : tpNode;
    ...
  end;

If you left the definition of tpNode until after tNode was defined,
you couldn't declare pNext.

FP

Other Threads