dynamic multidimensional arrays in c...

Started by
22 comments, last by earl3982 21 years, 8 months ago
how do i do dynamic multidimensional arrays in c ? ive seen how to do them in c++ but my project uses just c, no c++. thx in advance. stephen
Advertisement
anyone?
Identical, except you''ll be using malloc() and free() instead of new and delete. Read the docs on those functions.


Don''t listen to me. I''ve had too much coffee.
I suppose you could code your own clas... oh, only C? I meant you could code your own structure *wink wink* that functions as a dynamic array. Implementation isn''t hard. all you have to do is allocate the memory dynamical through class methods and overload the [] operator, but if you want multidimentional array support, I don''t know how you can accomodate for consecutive [] opreators (like array[10][5]). You might have to code a Get(...) function for that. Details are up to you.
Zipster, c structures cant have member functions. there is no operator overload or any fancy features of c++. most ppl compile code using a c++ compiler which treats all code as c++ (ie vc++) thus never actual see the restrictions of c.

allocating a multi dimensioanl array is easy, just search the forums for plenty of example code or both types (planar and linear).
quote:Original post by a person
Zipster, c structures cant have member functions. there is no operator overload or any fancy features of c++. most ppl compile code using a c++ compiler which treats all code as c++ (ie vc++) thus never actual see the restrictions of c.

allocating a multi dimensioanl array is easy, just search the forums for plenty of example code or both types (planar and linear).


A C structure can have a function pointer member, however, a function has to be assigned to it before it can be called.

int foo(int i, int j){ return i*j;}typedef int (*foo_t)(int,int);typedef struct tagMyStruct {  int x;  int y;  foo_t foofunc;} MyStruct;MyStruct mine;mine.foofunc = foo;// do stuff like assign values to mine.x and mine.yint z = mine.foo(mine.x, mine.y); 


As for the multidimensional arrays - the secret words are "pointer pointers"

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thank God I started programming after C++ came around, C sounds like it blowed! Heh
how about


  int width = something;int height = something;int x;int y;int* array = malloc(sizeof(int)*width*height);for(y=0;y<height;y++) { for(x=0;x<width;x++) {  array[x+width*y] = something(x,y); }}  


and, as a useful struct:


  typedef struct { int* data; int width; int height;} array2d;void set(array2d array,int x,int y,int data) { if(x<0||x>array.width) return; if(y<0||y>array.height) return; array.data[x+array.width*y] = data;}int get(array2d array,int x,int y,int* data) { if(x<0||x>array.width) return -1; if(y<0||y>array.height) return -1; *data = array.data[x+array.width*y]; return 0;}/*or*/int get_withouterror(array2d array,int x,int y) { if(x<0||x>array.width) return 0; if(y<0||y>array.height) return 0; return array.data[x+array.width*y];}  


this is fully with errorchecks, if you don''t want them, you can remove them, but its unsave then..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

quote:Original post by Zipster
Thank God I started programming after C++ came around, C sounds like it blowed! Heh


Um, yeah, that''s why it''s used to write os kernels and device drivers...

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I used to write device drivers for Windows in C++.
The Symbian OS for palm is written in C++.

This topic is closed to new replies.

Advertisement