Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

arrays in java

Name: Anonymous 2007-01-16 10:27

can someone tell me how to copy a 2 dimensional array to a 1 dimensional one? Ie.
array:
{2, 3, 5}
{4, 7, 9}
should become {2, 3, 5, 4, 7, 9}

Name: Anonymous 2007-01-18 4:08

>>16
Yes it has, though it still sucks


TYPE **a = malloc(WIDTH * sizeof(TYPE *));
for (int i = 0; i < WIDTH; i++) {
    a[i] = malloc(HEIGHT * sizeof(TYPE));
}


a is now a dynamic 2D array of TYPE

Name: Anonymous 2007-01-18 13:11

Hmm...

char **alloc2d(void) {
   unsigned int idx;
   char **block = malloc(rows * sizeof(*block));
   block[0] = malloc(cols * rows * sizeof(*block[0]));
   for( idx = 1U; idx < rows; idx++ ) {
      block[idx] = block[idx-1] + cols;
   }
   return block;
}


Now you can use the familiar block[x][y] syntax and deallocation is trivial:


void free2d(char **block) {
   free(block[0]);
   free(block);
}


I'm sure you knew that.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List