What does ^ mean?

Hi,

I wondered what the char ^ means in Delphi and where you are supposed to use it?

Regards,
Arash

Comments

  • : Hi,
    :
    : I wondered what the char ^ means in Delphi and where you are supposed to use it?
    :
    : Regards,
    : Arash
    : hi
    the " ^ "symbol dereferences a pointer or declares a variable as a pointer to some type. Example
    var p: ^Byte;
    this is pointer to a byte same as PByte.
    this construction is not as powerfull as in C
    my advice declare a type first don't create new types
    in variable declaration.

    p := AllocMem(1);//allocated 1 byte of memory
    //p is now pointing to a 1 byte memory chunk
    p^ := 10; //writting to memory through the pointer p;
    ...
    in most cases you can now omit the ^ symbol in assigment like p^:=10,
    (p:=10;//this should work)
    but ALWAYS use it when passing to a function.

  • Here's another example.

    Let's say that you have pointer P, which points to 255 bytes somewhere and you want to copy the byte 100 from there.

    procedure sample(P: Pointer);
    var
    B: Byte;
    begin
    B := pByte(Integer(P) + 100)^;
    end;

    Here's another way to do exactly same thing:

    procedure sample(P: Pointer);
    type
    TByteArray = array [0..255] of Byte;
    var
    ByteArray: ^TByteArray;//^ declares that this is pointer
    B: Byte;
    begin
    ByteArray := P;//ByteArray lets you now access to those 255 bytes
    B := ByteArray^[100];
    end;
  • Thanks for the grate information.

    Since I am a selflearned programmer, I am not so familiar with these kind of things. Can you please explain what a pointer is, and where it is good to use it (I know that PChar() has something to do with pointer, but I am not sure I understand it)

    Regards,
    Arash
  • : Thanks for the grate information.
    :
    : Since I am a selflearned programmer, I am not so familiar with these kind of things. Can you please explain what a pointer is, and where it is good to use it (I know that PChar() has something to do with pointer, but I am not sure I understand it)
    :
    : Regards,
    : Arash
    :
    A pointer is a reference to a place in the memory, in other words it points to a memory location. This makes in quite small (4 bytes), which allows programmers to create large lists of data.
    For example: Turbo Pascal can only create variables of 64kB or less. If you have an array of strings (256 B per string), you can make the array only 256 elements long. But since pointer are smaller an array of string-pointers could hold 16384 strings.
    A second advantage of pointers is that they are quick in their execution, because many variables are automatically converted to pointers by the compiler. So if speed is important, it helps to use pointers instead of other variables, although this will make your code very difficult to read.
    A third advantage is that pointers can be without a type. The variable type "pointer" is such an untyped variable. This means that it can point to a string, an integer, a complete array of records, or even a procedure or function. This makes it possible to write routines which perform a certain function on different variable types.

    A PChar is a so called null-terminated string. It points to the first of a "limitless" string of characters. This string is ended by the null-character (#0). It is different from Pascal strings, in that the string-type lists its actual length before the string itself. Before D2 the string type could only hold 255 characters and had its length in the zeroth character.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories