Jump to content
jrhodes

Initialize all array elements with data at time of array creation

Recommended Posts

Posted (edited)

It has been awhile since i tried to program on the c64, but i thought there was a way to specify all the initial values for a arrays elements?

In C++ we could do this:

int x[13] = {48,19,12,12,9,76,88,1,9,6,12,20,870,1,2,3,4}

 

Is there no way to do this on the c64?

I am aware i could use a loop and data statement, but that is not what i meant, or want.

I specifically seem to recall something similar to the above C++ example being doable on the c64, but i can not seem to get it to work.

I tried the following:

100 DIM X(12)=(48,19,12,12,9,76,88,1,9,6,12,20,87)

 

I am aware i could also do:

100 DIM X(12):X(0)=48:X(1)=19:X(2)=12:X(3)=12:X(4)=9:X(5)=76
101 X(6)=88:X(7)=1:X(8)=9:X(9)=6:X(10)=12:X(11)=20:X(12)=87

 

But this uses multiple lines, and not what i seem to recall being able to do.

Is my memory just cloudy, or am i mistaken and this just is not doable on the c64?

Edited by jrhodes

Share this post


Link to post
Share on other sites

No, there is no such support for initializing variables. Frankly I had never seen it before C++ and alike, does even regular C support it? I don't think Pascal does either. The FOR loop + DATA statement possibly is the best tradeoff for speed vs code size.

 

If you hack BASIC, there might be a way to point a variable to a defined set of data elsewhere in memory but I don't know how well that would work, since each number appears to be stored in floating point format once the interpreter has assigned it.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
On 7/26/2020 at 12:25 PM, carlsson said:

No, there is no such support for initializing variables. Frankly I had never seen it before C++ and alike, does even regular C support it? I don't think Pascal does either. The FOR loop + DATA statement possibly is the best tradeoff for speed vs code size.

 

If you hack BASIC, there might be a way to point a variable to a defined set of data elsewhere in memory but I don't know how well that would work, since each number appears to be stored in floating point format once the interpreter has assigned it.

 

Yes, even the original 'C' supports Loading the Arrays when they come into Scope.. 

here are some code examples from the ANSI Edition of the 'C' White Book:

  static char daytab[2][13] = {       {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},       {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}   };
	 

 static char *name[] = {       "Illegal month",       "January", "February", "March",       "April", "May", "June",       "July", "August", "September",       "October", "November", "December"  };
	 

Share this post


Link to post
Share on other sites

Probably the easiest way is to DIM the array then use a FOR/NEXT loop to READ the values from DATA statements.

 

In theory you could have a program which DIMs the array and loads the values, then use the variable table pointers to save the relevant memory locations.  Or, manually construct the variable table and values in a disk file.  I forget which book I found the layout and none of my books are on the shelf right now, but there is at least one book which explains how the variable table is constructed.

 

Later you can re-load the data with a LOAD"filename",8,1 (remember that BASIC restarts after a LOAD) and manually set the appropriate variable table pointers.

 

Honestly, a lot of work.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...