Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

earl3982

dynamic multidimensional arrays in c...

This topic is 5800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.y
int z = mine.foo(mine.x, mine.y);



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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I used to write device drivers for Windows in C++.
The Symbian OS for palm is written in C++.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!