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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.