C/C++ >> A C Question
Posted by Mintegra on 00:02:00 02-23-2002
I have a structure 'map'. In it I have two variable, width and height. I want to create a two dimensional array of integers, with the subscripts of width and height. So I would write:
int data[width][height]

Would this work? Another thing is the values of width and height are not know until after creating an instance of map. So would this code work?

struct map{
int height, width;
int data[width][height];
};

struct map castle;

map.height = 20;
map.width = 30;

If code like this would not work, which I am pretty sure it won't. What are my options for having similar functioning code? Basicly I want a structure that has an array of integers, where the subscript can be decided after it's declaration.

Thanks, JoshG.
Posted by KaGez on 09:10:00 02-28-2002
I think you should first dreate a 1x1 2 dimensiolan array, and then malloc afterwards. I dunno if it'd work, but that's the first idea that pops up inside me now
[addsig]
Posted by seunosewa on 17:56:00 03-31-2002
Below is a quick and dirty approach:


struct map {
int height, width;
int * data;
};

struct map create (int height, int width) {
struct map ret;
ret.height=height; ret.width=width;
data = calloc(sizeof(int), height*width);
}

int read (struct map * a, int h, int w) {
return (data[(a--->width)*h+j]);
}

void write (struct map * a, int h, int w, int val) {
data[(a--->width)*h+j] = val;
}


Useful?




[ This Message was edited by: seunosewa on 2002-03-31 17:57 ]
Posted by KaGez on 03:09:00 04-01-2002
no, because it isn't what he wants to do.
[addsig]
Posted by seunosewa on 20:27:00 04-01-2002
Well, if he's particular about the syntax and not just the functionality, Creating a 1x1 dimensional array, and mallocing afterwards, would not work. This may work:


////////////////////bfcode
struct map {
int height, width;
int ** data;
};

map createmap (int w, int h) {
map ret;
ret.height=h; ret.width=w;
ret.data = (int **)malloc(i);
for (int k=0; kw; k++) {
free((void *)m--->data[k]);
}
free ((void *)m--->data); m--->data=NULL;
}
////////////////////afcode


I cannot test it now, though.

[ This Message was edited by: seunosewa on 2002-04-01 20:29 ]
Posted by smog890 on 13:08:00 06-11-2002
I dont know what you really want to do. If you want the user to enter information and then store it in the array using malloc() then use linked list. Go here and download http://cslibrary.stanford.edu/ the pdf if you dont know what a linked list is.
Posted by MoX on 15:30:00 06-11-2002
Or go and read my ol' crappy tutorial on the topic [addsig]
Posted by Flikm on 09:00:00 06-17-2002
you should have a pointer to a pointer then allocate memory for it. the other approach would be C++ and STL ....
Posted by KaGez on 16:26:00 06-18-2002
using a vector would also be useful in this case
(caution: not the maths/physics vector )
[addsig]
Posted by MoX on 02:35:00 06-19-2002
Yep. The STL Vector should do fine. [addsig]
Posted by KaGez on 20:25:00 06-19-2002
btw, what does "STL" stand for anyways?
[addsig]
Posted by MoX on 02:19:00 06-20-2002
Standard Template Library

A collection of very useful templates.
[addsig]
Posted by KaGez on 16:21:00 06-21-2002
ahhh, ok, I'll keep that in my head
[addsig]