PDA

View Full Version : Pascal - Passing a Record as a Param.




larswik
May 10, 2011, 02:03 PM
So last night I had my last Pascal Class and Monday is the FINAL! I lost my book a month ago and finally got a new one 2 days ago and I am playing catchup.
By Monday I need to have read up on and understand
1. Arrays
2. Multi Dimensional Arrays
3. Records (which sounds like C Structs)

Each of these I get by there own for the most part. But combining them I get lost. Example, passing an ARRAY of RECORDS as a PARAMETER to a PROCEDURE

I was slowly working on some code and compiling as I went along and then hit a snag. I am getting a error in the PROCEDURE on this line Studentcard.idno[i] := 3345; . I am trying to assign that integer value 3345 to the record being stored in that array. I must be over looking something obvious.

program procpass;

const
numOfRecords = 30;


type
numOfRecordes = 1..30;
student = record
name : char;
major : integer;
classtpe : integer;
idno : integer;
end;
oneRecord = array[numOfRecordes] of student;

var

studentcard : oneRecord;

procedure inputdata(studentcard : oneRecord);
var
i : integer;
begin
for i := 1 to 30 DO
Studentcard.idno[i] := 3345;
end;
begin
inputdata(studentcard);

end.



Any help is appreciated :)

-Lars



McGordon
May 10, 2011, 02:17 PM
You didn't say what the error message said, which should have gave you a hint.

I have only got vague memories of pascal, but here goes...

You have an array of records, so to access a field in the record, you'd have to first access the record i.e. access an element of the array, then access its field.


studentcard[i].idno := 3345;


If its case insensitive, it wouldn't matter whether you use Studentcard or studentcard, but it doesn't look good if you change it every other time.

larswik
May 10, 2011, 02:49 PM
It is case insensitive and the error message I got was 'illegal Qualifier'

Thanks,

-Lars

gnasher729
May 10, 2011, 03:12 PM
It is case insensitive and the error message I got was 'illegal Qualifier'

Would be good to know on which line exactly the error is.

But your code won't work; you pass a record by value, so the global variable will not be modified. You probably want

procedure inputdata (var studentcard : oneRecord);

larswik
May 10, 2011, 04:53 PM
gnasher729 - Thanks, I found that out about 10 minutes ago looking through old notes. The var allows me to pass the address so I can get something out of that procedure.

I also fixed this
Studentcard.idno[i] := 3345;

to this
Studentcard[i].idno := 3345;

and it works now.

Thanks for your help.

-Lars

Bill McEnaney
May 10, 2011, 09:44 PM
So last night I had my last Pascal Class and Monday is the FINAL! I lost my book a month ago and finally got a new one 2 days ago and I am playing catchup.
By Monday I need to have read up on and understand
1. Arrays
2. Multi Dimensional Arrays
3. Records (which sounds like C Structs)

Each of these I get by there own for the most part. But combining them I get lost. Example, passing an ARRAY of RECORDS as a PARAMETER to a PROCEDURE

I was slowly working on some code and compiling as I went along and then hit a snag. I am getting a error in the PROCEDURE on this line Studentcard.idno[i] := 3345; . I am trying to assign that integer value 3345 to the record being stored in that array. I must be over looking something obvious.

program procpass;

const
numOfRecords = 30;


type
numOfRecordes = 1..30;
student = record
name : char;
major : integer;
classtpe : integer;
idno : integer;
end;
oneRecord = array[numOfRecordes] of student;

var

studentcard : oneRecord;

procedure inputdata(studentcard : oneRecord);
var
i : integer;
begin
for i := 1 to 30 DO
Studentcard.idno[i] := 3345;
end;
begin
inputdata(studentcard);

end.



Any help is appreciated :)

-Lars
The problem is probably that idno isn't an array. It's a component of an element of an array. Try:

Studentcard[i].idno := 3345;

Or,

with Studentcard[i] do
idno := 3345;