Here, i return back to array handling and manipulation. I wanted to work some more with passing arrays between functions to get a better handled on it. This program works with an array of arrays, something not quite possible in Action!.
First the program creates an array of 3 char arrays (strings). It prints the address of the array pointer, then prints each array element (string). Then it passes a pointer to the array and the number of elements to a function, which in turn prints each element (string) of the array.
Here is code
// ------------------------------------------------------------
// Program: arrays.c
// Desc...: Array demonstration
// Author.: Ripdubski
// Date...: 202208
// Notes..: cl65 -v -O -t atari arrays.c -o arrays.xex
// ------------------------------------------------------------
// Pull in include files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <atari.h>
// ------------------------------------------------------------
// Func...: void show(unsigned char **pcS, unsigned char cM)
// Desc...: Shows each array element
// Param..: pcS = pointer to array of strings
// cM = number of elements
// ------------------------------------------------------------
void show(unsigned char **pcS, unsigned char cM)
{
unsigned char cL;
printf(" # elements: %d\n", cM);
printf("pcS address: %p\n", pcS);
// Loop through each array element and print it
for (cL=0; cL<cM; cL++)
{
printf("Index %d: %s\n", cL, pcS[cL]);
}
}
// ------------------------------------------------------------
// Func...: void main(void)
// Desc...: Main routine
// ------------------------------------------------------------
void main(void)
{
// Loop var
unsigned char cL;
// Declare array of strings (non changeable)
unsigned char *cS[4] = { "Element 1", "Element 2", "Element 3" };
// Setup screen
clrscr();
printf("-[CC65 Array Tests]---------------------");
// Show strings from main loop
printf("\nStrings (main)\n");
printf(" cS address: %p\n", cS);
for (cL=0; cL<3; cL++)
{
printf("Index %d: %s\n", cL, cS[cL]);
}
// Manipulate string thru function
printf("\nStrings (function)\n");
show(cS, 3);
// Wait for a keypress
while (! kbhit()) { };
// Exit
return;
}
This is the command used to compile:
cl65 -v -t atarixl arrays.c -o arrays.xex
The is the build output:
$ cl65 -v -t atarixl arrays.c -o arrays.xex
Opened include file '/usr/local/share/cc65/include/stdio.h'
Opened include file '/usr/local/share/cc65/include/stddef.h'
Opened include file '/usr/local/share/cc65/include/stdarg.h'
Opened include file '/usr/local/share/cc65/include/conio.h'
Opened include file '/usr/local/share/cc65/include/atari.h'
Opened include file '/usr/local/share/cc65/include/_gtia.h'
Opened include file '/usr/local/share/cc65/include/_pbi.h'
Opened include file '/usr/local/share/cc65/include/_pokey.h'
Opened include file '/usr/local/share/cc65/include/_pia.h'
Opened include file '/usr/local/share/cc65/include/_antic.h'
Opened include file '/usr/local/share/cc65/include/string.h'
Opened include file '/usr/local/share/cc65/include/stddef.h'
Opened include file '/usr/local/share/cc65/include/atari.h'
0 errors, 0 warnings
Opened output file 'arrays.s'
Wrote output to 'arrays.s'
Closed output file 'arrays.s'
Opened 'arrays.xex'...
Dumping 'HEADER'
Writing 'EXEHDR'
Dumping 'SYSCHKHDR'
Writing 'SYSCHKHDR'
Dumping 'SYSCHKCHNK'
Writing 'SYSCHK'
Dumping 'SYSCHKTRL'
Writing 'SYSCHKTRL'
Dumping 'SRPREPHDR'
Writing 'SRPREPHDR'
Dumping 'SRPREPCHNK'
Writing 'LOWBSS'
Writing 'SRPREP'
Writing 'SHADOW_RAM'
Writing 'SHADOW_RAM2'
Dumping 'SRPREPTRL'
Writing 'SRPREPTRL'
Dumping 'MAINHDR'
Writing 'MAINHDR'
Dumping 'MAIN'
Writing 'STARTUP'
Writing 'LOWCODE'
Writing 'ONCE'
Writing 'CODE'
Writing 'RODATA'
Writing 'DATA'
Writing 'BSS'
Dumping 'TRAILER'
Writing 'AUTOSTRT'
Here is a screenshot of the running program:
