Subj : Re: only posting is rules To : Tony Master From : Scott Adams Date : Mon Jan 12 2004 12:28 am `07*** Quoting Scott Adams from a message to Tony Master ***`07 -=> Quoting Tony Master to Scott Adams <=- SA> SA> ML> TM> sad when only thing being posted is the rules frmo the SA> ML> TM> moderator. SA> ML> SA> ML> i agree... what can you contribute? SA> SA> TM> i don't teach. i learn =] SA> SA> So what do ya want to learn? TM> the best way to learn pointers for now SA> What level of knowledge do you know about them now? I Pulled out my old ancient (like 1995!) lessons on pascal. Keep in mind these were in a set of such lessons. I thought the one on pascal was more details but I find its just technical stuff from the language itself. I must've lost the other file with it. But maybe you'll get something out of it? :) Like I say though its 8 years old. Û²±° Pascal Lessons °±²Û o Advanced Topics þ Lesson 21: Pointers Pointers are probably th emost difficult to learn in any language. Pointers in basic terms are just constructs that point to something in a computer's memory. Think of a map. A street sign is a pointer which points to a street while addresses point to a particular slot within that street. Its the same analogy. I will try to explain the technical part of it but keep in mind something as short as this lesson can't cover it all. If you need further aid just ask and I can supply further discussion. Pointers are used because memory is a tight resource. IF you have a list of thousands of strings for example its futile and imposible to keep it flowing as memory will run out. Thus you should use pointers to point to items in that list. This then only uses ONE record (of that byte size) instead of the thousnads of records and their byte size total. Now for the technicals: The @ ("at") operator: Pointer operation ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß You can create a pointer to a variable with the @ operator. Operator³Operation ³Operand types ³Result type ÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ @ ³Pointer formation³Variable reference, ³Pointer (same as nil) ³ ³procedure identifier, ³ ³ ³or function identifier³ @ is a unary operator. Special rules apply to use of the @ operator with a procedural variable. The type of the value is the same as the type of nil, so it can be assigned to any pointer variable. @ with a variable: Using @ with an ordinary variable (not a parameter) is not complicated. For example, given these declarations: type TwoChar = array[0..1] of Char; var Int: Integer; TwoCharPtr: ^TwoChar; this statement causes TwoCharPtr to point to Int: TwoCharPtr := @Int; TwoCharPtr^ becomes a reinterpretation of the value of Int, as if it were an array[0..1] of Char. @ with a value parameter: Applying @ to a formal value parameter results in a pointer to the stack location containing the actual value. For example, suppose Fred is a formal value parameter in a procedure and FredPtr is a pointer variable. If the procedure executes this statement FredPtr := @Fred; FredPtr^ references Fred's value. However, FredPtr^ does not reference Fred itselfit references the value that was taken from Fred and stored on the stack. @ with a variable parameter: Applying @ to a formal variable parameter results in a pointer to the actual parameter (the pointer is taken from the stack). The type of resulting pointer value is controlled through the $T compiler directive. For example, suppose the following: - One is a formal variable parameter of a procedure, - Two is a variable passed to the procedure as One's actual parameter - OnePtr is a pointer variable. If the procedure executes this statement OnePtr := @One; OnePtr is a pointer to Two, and OnePtr^ is a reference to Two itself. @ with a procedure or function: You can apply @ to a procedure or a function to produce a pointer to its entry point. Turbo Pascal does not give you a mechanism for using such a pointer. The only use for a procedure pointer is to pass it to an assembly language routine or to use it in an inline statement. @ with a method: You can apply @ to a qualified method identifier to produce a pointer to the method's entry point. Pointer-type constants ßßßßßßßßßßßßßßßßßßßßßßßß The declaration of a pointer-type constant typically uses a constant address expression to specify the pointer value. When you enable the extended syntax (with a {$X+} compiler directive), a typed constant of type PChar can be initialized with a string constant. Examples: type Direction = (Left, Right, Up, Down); StringPtr = ^String; NodePtr = ^Node; Node = record Next: NodePtr; Symbol: StringPtr; Value: Direction; end; const S1: string[4] = 'DOWN'; S2: string[2] = 'UP'; S3: string[5] = 'RIGHT'; S4: string[4] = 'LEFT'; N1: Node = (Next: nil; Symbol: @S1; Value: Down); N2: Node = (Next: @N1; Symbol: @S2; Value: Up); N3: Node = (Next: @N2; Symbol: @S3; Value: Right); N4: Node = (Next: @N3; Symbol: @S4; Value: Left); DirectionTable: NodePtr = @N4; Pointer types ßßßßßßßßßßßßßß A pointer type variable contains the memory address of a dynamic variable of a specified base type. You can assign a value to a pointer variable with: - the New or GetMem procedures - the @ operator - the Ptr function The reserved word nil denotes a pointer constant that does not point to anything. Pointer: The predefined type Pointer denotes an untyped pointer (a pointer that does not point to any specific type). PChar: The predefined type PChar denotes a pointer to a null-terminated string. PChar is declared as: type PChar = ^Char; Borland Pascal for Windows supports a set of extended syntax rules (controlled by the $X compiler directive) to facilitate handling of strings using the PChar type. Example: { Pointer Type Declaration } type BytePtr = ^Byte; WordPtr = ^Word; IdentPtr = ^IdentRec; IdentRec = record Ident: string[15]; RefCount: Word; Next: IdentPtr; end; Don't be worried if you don't understand the above at first. It will need to sink in with various examples. I can furish them if needed. ---- Looking it over its not that useful :) --- Fringe BBS * Origin: EWOG II - The Fringe - 904-733-1721 (1:112/91) .