Pointer to array of record type - Programmers Heaven

Howdy, Stranger!

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

Categories

Pointer to array of record type

ieriieri Posts: 22Member
Hi,
I have a problem with pointer variables which points to array of a record type.
Example:

[code]
//data types and variables:
type
TRec1 = record
f1: String;
f2: Integer;
end;
TArr1 = array [0..10] of TRec1;
var
pArr : ^TArr1;
aArr : TArr1;

//any code block:

aArr[0].f1 := 'xxx';//no problem!!
pArr[0].f1 := 'yyy'; //Access Violation error!!!
[/code]
Delphi compiles the above code, so it took me a lot of time where the problem is.

this may be a simple question but I couldnt find a solution.
How can I assign values to or read values from pArr?
Pls help me!!!

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi,
    : I have a problem with pointer variables which points to array of a record type.
    : Example:
    :
    : [code]
    : //data types and variables:
    : type
    : TRec1 = record
    : f1: String;
    : f2: Integer;
    : end;
    : TArr1 = array [0..10] of TRec1;
    : var
    : pArr : ^TArr1;
    : aArr : TArr1;
    :
    : //any code block:
    :
    : aArr[0].f1 := 'xxx';//no problem!!
    : pArr[0].f1 := 'yyy'; //Access Violation error!!!
    : [/code]
    : Delphi compiles the above code, so it took me a lot of time where the problem is.
    :
    : this may be a simple question but I couldnt find a solution.
    : How can I assign values to or read values from pArr?
    : Pls help me!!!
    :
    First you need to dereference the pointer to get to the array. Then you can access each element like a normal array. In code:
    [code]
    pArr^[0].f1 := 'something';
    [/code]
    Beware: [red]You also need to create the pointer using New(), otherwise it will be a nil pointer or point to some memory address. The latter can generate the most interesting errors, like completely locking your system.[/red]
Sign In or Register to comment.