Pascal - Passing a Record as a Param.

Discussion in 'Mac Programming' started by larswik, May 10, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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 := 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.

    Code:
    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
     
  2. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    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.

    Code:
    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.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    It is case insensitive and the error message I got was 'illegal Qualifier'

    Thanks,

    -Lars
     
  4. gnasher729 macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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

    Code:
    procedure inputdata (var studentcard : oneRecord);
    
     
  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    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 := 3345;

    to this
    Studentcard.idno := 3345;

    and it works now.

    Thanks for your help.

    -Lars
     
  6. Bill McEnaney, May 10, 2011
    Last edited: May 10, 2011

    Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #6

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

    Code:
    Studentcard[i].idno := 3345;
    Or,

    Code:
    with Studentcard[i] do
       idno := 3345;
     

Share This Page