Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multi-dim arrays in c



 On Fri, Aug 29, 2008 at 05:43:40PM -0400, Stephen Adler wrote: 
> Without doing pointer arithmatic, is it possible to use the standard 
> [][] notation somehow? 

If you really don't want to use pointer arithmetic, you can write a 
function to convert your multi-dimensional array into a 
single-dimensional array.  What is logically represented as a 
multi-dimensional array in C is in actuality still a linear sequence 
of bytes (or words) in memory.  The converted index is computed as: 

  row * #columns + column 

int get_index(int x, int y, int columns){ 
        return columns * y + x; 
} 

You could also write a getter and setter function that uses this.  If 
we're bothering with all this, we prolly also want to check if we used 
values of x and y which are out of bounds for our array, so we'll use 
another variable to get the value, and pass its address to store the 
answer.  The return value is -1 if x or y are out of bounds, 0 
otherwise. 

int get_item(int x, int y, int columns, int rows, int *rc, int *data) 
{ 
        if ( x >= columns || y >= rows) return -1; 
        *rc = data[get_index(x,y,columns)]; 
        return 0; 
} 

int set_item(int x, int y, int columns, int rows, int value, int *data) 
{ 
        if ( x >= columns || y >= rows) return -1; 
        data[get_index(x,y,columns)] = value; 
        return 0; 
} 

Not exactly what you asked for, but it works. 
  
Note that in your example, data[y][x] is outside the range of your 
array, which has rows 0 to y-1, columns 0 to x-1.  Thus this line is 
an error (plus it wouldn't have been the middle element). 

Here's a main function that uses the functions, more or less a copy of 
what you wrote: 

int main( int argc, char **argv ) 
{ 
        int *data; 
        int value, x, y, i, j; 
        
        x=3; 
        y=3; 
        data=(int *)malloc(x*y*sizeof(int)); 

        /* populate the array */ 
        for (i = 0; i < x; i++) 
                for (j = 0; j < y; j++) 
                        set_item(i,j,x,y,(i+1) * (j+1), data); 

        /* 
         * This may or may not truly yeild the middle element... 
         * there may not be any middle element, depending on the 
         * values of x and y. 
         */ 
        if (get_item(x/2,y/2,x,y,&amp;value,data)){ 
        fprintf(stderr, "Index out of range\n"); 
                return 1; 
      } 
        printf("Middle data element is %d\n", value); 

        /* print the array (matrix) */ 
        for (i = 0; i < x; i++){ 
                for (j = 0; j < y; j++){ 
                        get_item(i,j,x,y,&amp;value, data); 
                        printf("%3d ", value); 
                } 
                printf("\n"); 
        } 

        return 0; 
} 

Slap on an #include <stdio.h> and compile: 

$ gcc -o foo foo.c 
$ ./foo 
Middle data element is 4 
  1   2   3 
  2   4   6 
  3   6   9 


-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02 
-=-=-=-=- 
This message is posted from an invalid address.  Replying to it will result in 
undeliverable mail due to spam prevention.  Sorry for the inconvenience. 

_______________________________________________ 
Discuss mailing list 
[hidden email] 
http://lists.blu.org/mailman/listinfo/discuss

 foo.c (1K) Download Attachment


BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org