Skip to main content
gamedev.net
🔒 Locked

Array problem

Started by Jaxks 4 replies 400+ views
Original Post
Jaxks
Jaxks
    
class someclass
{
public:
   int Give_me_integer()const{return integer;}
   void LoadFile(char *);
private:
   int integer;
};
void Someclass::LoadFile(char * file)
{
  //opens filee...

  unsigned char array[integer][integer];
     
Defination of Array makes some errors because integer doesn´t have no value. I´ve following situation in my Bmp loader which loads height and widht from file. How make such array or this unsigned char * data; data=new unsigned char[width][heihgt]; Edited by - Jaxks on 6/29/00 6:04:32 AM Edited by - Jaxks on 6/29/00 6:05:30 AM Edited by - Jaxks on 6/29/00 6:06:01 AM
Armitage
Armitage
You'll have to initialize it dynamically, yes.

        
unsigned char **data;
data = new unsigned char*[width];
for (int i = 0; i < width; i++)
{
data<i> = new unsigned char[height];
}


I've probably mixed some things up, I usually check my manual for this, but I'm at work.
But why not use "new unsigned char[width * height]" and manage the offsets yourself?



Edited by - Armitage on June 29, 2000 7:45:24 AM
A polar bear is a rectangular bear after a coordinate transform.
gavcc
gavcc
The reason you are getting errors is beacuase you can only define an array using constant values.

Simple using


int i = 100
char array

or

char array[(const int )i]


will not do. One way around this is to allocate memory to your array, then you can use your variable Integer:


char* array = new char[Integer][Integer]


You could always declate Integer as const int Integer, but you will not be able to change the value.

Regards
Michael

Edited by - gavcc on June 29, 2000 7:49:01 AM
Jeranon
Jeranon
I''m making a stab in the dark here...

Vectors?
JeranonGame maker wannabe.
MikeD
MikeD
You could write your own dynamic unsigned char array class and do all the data allocation, manipulation and extraction in internal member functions.

Kinda like

class myarray
{
public:
myarray(int width, int height);
getelement(int x, int y);

private:
unsigned char* array;
int width, height;
}

myarray* myarray::myarray(int newwidth, int newheight)
{
malloc(sizeof(unsigned char) * newwidth * newheight);
width = newwidth;
height = newheight;
return this;
}

unsigned char myarray::getelement(int x, int y)
{
return (unsigned char) *(array + (x * width) + y);
}

Obviously you can override the new operator to make it more c++ like but that''s ''a'' way of doing it.

Mike
Jaxks
Jaxks
Can´t do constants because information is red from file.
And armitage if I make two dimensional dynamical array how to initialize via two fors? my book is so old so i haven´t heard anything about it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.