AllExperts > Experts 
Search      

Pascal

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Pascal Answers
Question Library

Ask a question about Pascal
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Francis Kong
Expertise
Pascal programming. I would be best at solving logic and syntax errors with some given code, or some programming theory and specific task. I have done mostly my pascal within a machine(meaning, not networking and serial cable yet)and I am not able to solve things in ObjectWindows... I should be able to answer most of the thing done in console(dos and x systems like linux) platform and in Mac(but for Mac I can only handle nonplatform-specific questions). Feel free to ask me, because if I am not able to answer it I might be able to tell where to get the specific material that you need

Experience
teached a classmate with an exam mark of 56% and the second shot with a higher difficulty the tutee gets more than 80%
 
   

You are here:  Experts > Computing/Technology > Pascal > Pascal > arrays in Turbo Pascal

Topic: Pascal



Expert: Francis Kong
Date: 1/22/2004
Subject: arrays in Turbo Pascal

Question
How can I make an array with a variable index type in Turbo Pascal?
for example sth like this:
Var x:byte;
   ar:array[1..x] of Char;

Answer
you don't.  for variable length array (not that you'll really need them anyways, just make a static length long enough for the maximum case) , try use the following:

in the declaration:

var p,q: ^char;
   k: integer;


in the program:
before you try to use the array, use getmem to initialize it like (the following has no error checks, so if you need some of those, look into the help file):

{get a value of k from input or assignment}
getmem(p,k*sizeof(char));


to access the array, try this:

q := p; {the first element}
q^ := 'A'{or whatever you like}
inc(q); {This goes to the next one, look into "inc" and "dec" in help for more details}

to cleanup, use the following:

freemem(p,k*sizeof(char));  

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.