MESSAGE
DATE | 2008-08-30 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] Re: multi-dim arrays in c
|
On Fri, Aug 29, 2008 at 08:16:35PM -0400, Ruben Safir wrote: > On Fri, Aug 29, 2008 at 05:43:40PM -0400, 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? > > > > Thanks. > > > nclude #include
int **my; int x = 100; int y = 500; int i = 0; int j = 0; int k = 0; int main(){ my = (int **)malloc(x * sizeof(int*));
for(; i my[i] = (int *)malloc(y * sizeof(int *)); }
for(i = 0; i < x; i++) for(j=0; j my[i][j] = i + j; printf ("i=> %d j=> %d slot: %d ==> %d\n",i,j,k,my[i][j]); k++; printf("Hello\n"); }
printf("%d", my[0][5]);
return 0;
}
> > Yes, in fact int main() does this using char** as a parameter. > > I have a sample worked out on my laptop which I can't put on my > jobs network, but when I get home I'll post an example. > > Ruben > > > > > > -- > > This message has been scanned for viruses and > > dangerous content by MailScanner, and is > > believed to be clean. > > > > _______________________________________________ > > Discuss mailing list > > Discuss-at-blu.org > > http://lists.blu.org/mailman/listinfo/discuss > > -- > http://www.mrbrklyn.com - Interesting Stuff > http://www.nylxs.com - Leadership Development in Free Software > > So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 > > http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 > > "Yeah - I write Free Software...so SUE ME" > > "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society." > > "> I'm an engineer. I choose the best tool for the job, politics be damned.< > You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one." > > © Copyright for the Digital Millennium > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean. > > _______________________________________________ > Discuss mailing list > Discuss-at-blu.org > http://lists.blu.org/mailman/listinfo/discuss
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
© Copyright for the Digital Millennium
|
|