C的指针可用下标,以达到数组的功能,并可与数组互换。可以与整数进行加减运算来进行寻址。如:int *pInt; int a_int[10];pInt = a_int; // 与指针互换 pInt[2] = 10; // 下标寻址,可以用下标存取某一元素 pInt += 4; // 地址变换Delphi中除了PChar可以达到这一点,其它的指针不行。如: type MyType = LongWord;var a: array [0..10] of MyType; P: ^MyType; begin P := a; // Incompatible types: 'Array' and 'Pointer' P := P+2; // Operator not applicable to this operand type P[1] := 100; // Array type required
虽然pascal对变量要求严格,可是却不失其灵活性,掌握的好,同样可以做到游刃有余的地步,不会感到有任何的限制。好不好用只在于谁用,不在于用谁。
int a_int[10];pInt = a_int; // 与指针互换
pInt[2] = 10; // 下标寻址,可以用下标存取某一元素
pInt += 4; // 地址变换Delphi中除了PChar可以达到这一点,其它的指针不行。如:
type
MyType = LongWord;var
a: array [0..10] of MyType;
P: ^MyType;
begin
P := a; // Incompatible types: 'Array' and 'Pointer'
P := P+2; // Operator not applicable to this operand type
P[1] := 100; // Array type required
但我后来发现对指针的加减可以用Inc()和Dec()来达到。的确,C指针容易让人犯错,所以要用来必须有很深的功力。// 我几乎都用Delphi编程,也很喜欢Pascal,以上纯粹是学术探讨。
// 不要对我扔砖头。(因为我发现在这里很容易因为工具和语言打起来)
MyType=LongWord;
PMyType=^MyType;
var
a: array [0..10] of MyType;
p: PMyType;begin
p:=@a; //与指针互换
PMyType(longint(p)+sizeof(MyType)*3)^:=10; //与直接使用下标寻址效果相同,但稍微麻烦一些
writeln(a[3]);
inc(p,3); //地址变换
p^:=1; //通过指针改变数组元素的值
writeln(a[3]);
readln;
end.在Pascal/Delphi中,虽然不能对指针直接使用下标存取数组元素,但可以通过一些间接的方式进行。虽然一般用不到这点,但是用到的话,也不是不可能。