[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: dynamic arrays
From: |
Marcus G. Daniels |
Subject: |
Re: dynamic arrays |
Date: |
20 May 1998 08:04:49 -0700 |
>>>>> "JM" == James Marshall <address@hidden> writes:
JM> I want to create an array of integers at runtime... in C++ I'd do
JM> the following:
int *array, xsize, ysize;
xsize=10;
ysize=10;
array=new int[xsize][ysize];
JM> How do I do this with Swarm and objective C? From my fiddlings it
JM> seems alloc is replaced by create... can you use the create message
JM> with the default types?
Also, since Objective C is based on GNU C, you can have dynamically
sized arrays, and closure over variables (e.g. xsize)...
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
main (int argc, char **argv)
{
size_t xsize, ysize, xi, yi;
int *permanent_array;
{
void print (size_t xi, size_t yi, int array[][xsize])
{
printf (" %d", array[yi][xi]);
}
if (argc < 3)
{
fprintf (stderr, "Specify X and Y dimensions\n");
exit (1);
}
xsize = atoi (argv[1]);
ysize = atoi (argv[2]);
{
int temporary_array[ysize][xsize];
for (yi = 0; yi < ysize; yi++)
for (xi = 0; xi < xsize; xi++)
temporary_array[yi][xi] = xi * yi;
permanent_array = malloc (sizeof (temporary_array));
if (permanent_array == NULL)
abort ();
memcpy (permanent_array, temporary_array, sizeof (temporary_array));
}
for (yi = 0; yi < ysize; yi++)
{
for (xi = 0; xi < xsize; xi++)
print (xi, yi, (void *)permanent_array);
putchar ('\n');
}
}
}
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
==================================