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 | 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 08/29/2008 05:43 PM, Stephen Adler wrote: 
> Guys, 
> 
> I've got a standard c coding question which has been nagging me for a 
> while and I was wondering what you guys may think or have the correct 
> solution to this problem.... 
> 
> Lets say I have a 2 dim array of data and the dimension sizes can 
> change depending on the specific data set. I can read in the x and y 
> dimensions from a file, then read in the x*y elements of the data a 
> populate a memory area x*y*elementsizeinbytes with the data. But now I 
> want to access the data. Without doing pointer arithmatic, is it 
> possible to use the standard [][] notation somehow? 
> 
> for example 
> 
>    int *data; 
>    x=getXDim(); 
>    y=getYDim(); 
>    data=(int *)malloc(x*y*sizeof(int)); 
>    readInData(x,y,data); 
>    printf("Middle data element is %d\n",data[y][x]); 
> 
> but I know that that data[y][x] will barf on the compiler because the 
> compiler has no way of knowing what the y size of the "y" dimension is... 
> 
> Any suggestions on how to use the N-dimensional bracket notation in 
> this situation? 


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