turbo pascal pointer 
Author Message
 turbo pascal pointer

I'm really new to TP, so this may be a bit basic!
Say you've got two arrays of a given type, for instance BYTE, and you need
to have a pointer that can point to any one of these arrays depening on
different variables. I'll try to give an ex.

if  -something- then
  p:=pointerto(array1)
else
  p:=pointerto(array2);

variable:=getbyteatposition(p+offset);

Is this possible??
I hope anyone can help me!!

Thanks!



Wed, 18 Jun 1902 08:00:00 GMT  
 turbo pascal pointer
Hi,

Quote:

> Say you've got two arrays of a given type, for instance BYTE, and you need
> to have a pointer that can point to any one of these arrays depening on
> different variables. I'll try to give an ex.

> if  -something- then
>   p:=pointerto(array1)
> else
>   p:=pointerto(array2);

> variable:=getbyteatposition(p+offset);

> Is this possible??


important part, however, is that p should be declared as a pointer to
the appropriate array type. Example:

   program pta;

   type
      ArrType = array[1..5] of Char;

   var
      a1, a2 : ArrType;
      p : ^ArrType;
      i : Integer;

   begin

      for i := 1 to 5 do p^[i] := Chr(Ord('A') + i - 1);

      for i := 1 to 5 do p^[i] := Chr(Ord('a') + i - 1);
      for i := 1 to 5 do Write(a1[i], a2[i]);
      Writeln;
   end.

Hope this helps,

 - Sebastian



Wed, 18 Jun 1902 08:00:00 GMT  
 turbo pascal pointer

Quote:

>I'm really new to TP, so this may be a bit basic!
>Say you've got two arrays of a given type, for instance BYTE, and you need
>to have a pointer that can point to any one of these arrays depening on
>different variables. I'll try to give an ex.

>if  -something- then
>  p:=pointerto(array1)
>else
>  p:=pointerto(array2);

>variable:=getbyteatposition(p+offset);

>Is this possible??
>I hope anyone can help me!!

>Thanks!

Yes I think so.

This is of the top of my head so hope hope someone tells me if this is wrong
but here goes:

var
  one,two:array[1..100] of integer;

 p:^integer;  {declares an pointer}

begin
  if -boolean expression- then
    p := addr(one[1])
 else
     p:= addr(two[1]);

Know to write element X of the array to the screen:
inc(p,X-1);
write(p^);
dec(p,X-1);

Yes I know there other ways to do this.
Probably better ways too :-D

Chaou
Heinrich

Check my web-site:
http://homes.arealcity.com/Heinrichshomepage/
Includes a complete TP tutoriul and more!



Wed, 18 Jun 1902 08:00:00 GMT  
 turbo pascal pointer


Quote:
>I'm really new to TP, so this may be a bit basic!
>Say you've got two arrays of a given type, for instance BYTE, and you need
>to have a pointer that can point to any one of these arrays depening on
>different variables. I'll try to give an ex.

>if  -something- then
>  p:=pointerto(array1)
>else
>  p:=pointerto(array2);

>variable:=getbyteatposition(p+offset);

>Is this possible??
>I hope anyone can help me!!

>Thanks!

{ A 'pointer' has a specific meaning in Pascal which clouds the
interpretation  of your question. However, let me guess at a prototype
that would fit your question. Assume you want to send messages in one
of 3 simple character substitution codes. These codes may be
represented as arrays of various orders of 'A'..'Z', for simplicity.
The first element of these arrays always represents 'A', the second
'B' and so on to the 26th element representing 'Z'. You can pass any
of these arrays as a parameter to a FUNCTION or PROCEDURE by defining
them as a USER TYPE, something like this:  }

Program PrimitiveCode;
CONST alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
TYPE  mycode = Array[1..26] of Char;
VAR
codeArray, code1, code2, code3, plain:mycode;
s:String;
j:Integer;

Procedure Offset(VAR codeX:mycode; k:Integer);
VAR ct :Integer;
    alpha:String[26];
Begin
     alpha := alphabet;
     For ct := 1 to 26 Do codeX[ct] := alpha[1 + (ct + k) MOD 26];
End;

Procedure Cypher(VAR secret:String; codeA, codeB:mycode);
VAR ct, p:Integer;
    ss:String[26];
Begin
     Writeln('Characters input:':30);
     Writeln(secret);
     ss := '';
     For ct := 1 to 26 Do ss := ss + codeA[ct];  {string}
     For ct := 1 to Length(secret) DO
     Begin
          p := Pos(secret[ct], ss);  {position of char in ss}
          If p > 0 then secret[ct] := codeB[p];  {char substitution}
     End;
     Writeln('Characters output':30);
     Writeln(secret);
End;

Begin
     Writeln;  Writeln;
     Offset(plain, -1);   {making arrays}
     Offset(code1, 5);
     Offset(code2, 10);
     Offset(code3, 15);
                           {show arrays}
     For j := 1 to 26 Do Write(plain[j]); Writeln;
     For j := 1 to 26 Do Write(code1[j]); Writeln;
     For j := 1 to 26 Do Write(code2[j]); Writeln;
     For j := 1 to 26 Do Write(code3[j]); Writeln; Writeln;

     s := 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';
     Cypher(s, plain, code1);
     Cypher(s, code1, plain);

Repeat
     Writeln;
Writeln('Enter words below using capitals, (just <Enter> quits): ');
     Readln(s);
     If s = '' then EXIT;
     Write('Enter code number (1..3): ');  Readln(j);
     CASE j of
          1:codeArray := code1;
          2:codeArray := code2;
          3:codeArray := code3;
     End;

     Cypher(s, plain, codeArray);
     Cypher(s, codeArray, plain);
Until s = '';
End.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Turbo Pascal pointers

2. Turbo Pascal Frequently Asked Questions, a monthly pointer

3. Turbo Pascal Frequently Asked Questions, a monthly pointer

4. Turbo Pascal Frequently Asked Questions, a monthly pointer

5. Turbo Pascal Frequently Asked Questions, a monthly pointer

6. Turbo Pascal Frequently Asked Questions, a monthly pointer

7. Turbo Pascal Frequently Asked Questions, a monthly pointer

8. Turbo Pascal Frequently Asked Questions, a monthly pointer

9. Turbo Pascal Frequently Asked Questions, a monthly pointer

10. Turbo Pascal Frequently Asked Questions, a monthly pointer

11. Turbo Pascal Frequently Asked Questions, a monthly pointer

12. Turbo Pascal Frequently Asked Questions, a monthly pointer

 

 
Powered by phpBB® Forum Software