Arrays

I made a structure. Here's my code:

struct IMAGE
{
char *filename;
int w;
int h;
void setwh(int inputw,int inputh)
{
h=inputh;
w=inputw;
}
int data[width][height];
};

Is there any way to define width and height when the struct is create. For example:

IMAGE hi.setwh(10,10);

Will that make the data array data[10][10]? How can I make it do that?

Comments

  • I just compiled my code. I get an error with declaring the data array.

    Let's try this code:

    struct IMAGE
    {
    char *filename;
    int w;
    int h;
    int data[/*Number set when a IMAGE variable is create*/[/*Another number set when a IMAGE variable is create*/];
    };

    How can i define those 2 numbers when an IMAGE variable is created?


  • [b][red]This message was edited by MT2002 at 2005-6-16 16:26:41[/red][/b][hr]
    : I just compiled my code. I get an error with declaring the data array.
    :
    : Let's try this code:
    :
    : struct IMAGE
    : {
    : char *filename;
    : int w;
    : int h;
    : int data[/*Number set when a IMAGE variable is create*/[/*Another number set when a IMAGE variable is create*/];
    : };
    :
    : How can i define those 2 numbers when an IMAGE variable is created?
    [blue]
    Arrays can only be created with constants. I [b]think[/b]
    what you want is to dynamically allocate 'data' on the heap:[/blue]
    [code]
    int** data;
    [green]
    // width and height are passed into function...

    // allocate 2d array[/green]
    data=(int**)malloc (sizeof(int*)*height);
    for (int i=0; i<height;i++)
    data[i]=(int*)malloc (sizeof(int)*width);
    [/code][blue]
    This could be made more easier with new and delete.

    This is the bases of dynamic data structures that
    can grow/shrink dynamically at runtime.[/blue]

  • : I just compiled my code. I get an error with declaring the data array.
    :
    : Let's try this code:
    :
    : struct IMAGE
    : {
    : char *filename;
    : int w;
    : int h;
    : int data[/*Number set when a IMAGE variable is create*/[/*Another number set when a IMAGE variable is create*/];
    : };
    :
    : How can i define those 2 numbers when an IMAGE variable is created?
    :
    :
    :


    [blue]
    Can you use C++ or is this C only. If you use C++ you can take advantage of templates to do this:

    [code]#include
    using namespace std;

    template
    struct image
    {
    int data[height][width];
    image(int init=0)
    {
    for( int h = 0; h < height; ++h )
    for( int w = 0; w < width; ++w )
    data[h][w] = init;
    }
    int Width() const
    {
    return width;
    }
    int Height() const
    {
    return height;
    }
    };

    int main()
    {
    image<> img1; // Create a default 5x5 array initialized to 0
    image<6,2> img2(5); // Create a custom 6x2 array initialized to 5
    int i, j;

    // Show some stats for img1
    cout << "img1 - sizeof: " << sizeof(img1)
    << " height: " << img1.Height()
    << " width: " << img1.Width()
    << endl;
    for( i = 0; i < img1.Height(); ++i )
    {
    for( j = 0; j < img1.Width(); ++j )
    cout << img1.data[i][j] << ' ';
    cout << '
    ';
    }

    // Show some stats for img2
    cout << "img2 - sizeof: " << sizeof(img2)
    << " height: " << img2.Height()
    << " width: " << img2.Width()
    << endl;
    for( i = 0; i < img2.Height(); ++i )
    {
    for( j = 0; j < img2.Width(); ++j )
    cout << img2.data[i][j] << ' ';
    cout << '
    ';
    }

    return 0;

    }[/code]

    Output on my machine is:
    [code]img1 - sizeof: 100 height: 5 width: 5
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    img2 - sizeof: 48 height: 6 width: 2
    5 5
    5 5
    5 5
    5 5
    5 5
    5 5
    [/code]

    I was going to overload the stream insertion operator (<<) to make the output easier (so I wouldn't have those two nested [italic]for[/italic] loops) but I forgot how for the moment.[/blue]
  • [b][red]This message was edited by HK_MP5KPDW at 2005-6-18 15:12:22[/red][/b][hr]
    : [blue]
    : Can you use C++ or is this C only. If you use C++ you can take advantage of templates to do this:
    :
    : [code]#include
    : using namespace std;
    :
    : template
    : struct image
    : {
    : int data[height][width];
    : image(int init=0)
    : {
    : for( int h = 0; h < height; ++h )
    : for( int w = 0; w < width; ++w )
    : data[h][w] = init;
    : }
    : int Width() const
    : {
    : return width;
    : }
    : int Height() const
    : {
    : return height;
    : }
    : };
    :
    : int main()
    : {
    : image<> img1; // Create a default 5x5 array initialized to 0
    : image<6,2> img2(5); // Create a custom 6x2 array initialized to 5
    : int i, j;
    :
    : // Show some stats for img1
    : cout << "img1 - sizeof: " << sizeof(img1)
    : << " height: " << img1.Height()
    : << " width: " << img1.Width()
    : << endl;
    : for( i = 0; i < img1.Height(); ++i )
    : {
    : for( j = 0; j < img1.Width(); ++j )
    : cout << img1.data[i][j] << ' ';
    : cout << '
    ';
    : }
    :
    : // Show some stats for img2
    : cout << "img2 - sizeof: " << sizeof(img2)
    : << " height: " << img2.Height()
    : << " width: " << img2.Width()
    : << endl;
    : for( i = 0; i < img2.Height(); ++i )
    : {
    : for( j = 0; j < img2.Width(); ++j )
    : cout << img2.data[i][j] << ' ';
    : cout << '
    ';
    : }
    :
    : return 0;
    :
    : }[/code]
    :
    : Output on my machine is:
    : [code]img1 - sizeof: 100 height: 5 width: 5
    : 0 0 0 0 0
    : 0 0 0 0 0
    : 0 0 0 0 0
    : 0 0 0 0 0
    : 0 0 0 0 0
    : img2 - sizeof: 48 height: 6 width: 2
    : 5 5
    : 5 5
    : 5 5
    : 5 5
    : 5 5
    : 5 5
    : [/code]
    :
    : I was going to overload the stream insertion operator (<<) to make the output easier (so I wouldn't have those two nested [italic]for[/italic] loops) but I forgot how for the moment.[/blue]
    :

    Okay, I managed to figure out overloading of the stream insertion operator with a templated class after about half an hour of trying various combos. Here it is if anyone is interested with the new stuff in [blue]blue[/blue]:

    [code]#include <iostream>
    using namespace std;

    template
    struct image
    {
    int data[height][width];
    image(int init=0)
    {
    for( int h = 0; h < height; ++h )
    for( int w = 0; w < width; ++w )
    data[h][w] = init;
    }
    int Width() const
    {
    return width;
    }
    int Height() const
    {
    return height;
    }
    };

    [blue]template
    ostream& operator<<(ostream& os, const image<T1,T2>& rhs)
    {
    for( int i = 0; i < rhs.Height(); ++i )
    {
    for( int j = 0; j < rhs.Width(); ++j )
    os << rhs.data[i][j] << ' ';
    os << '
    ';
    }
    return os;
    }[/blue]

    int main()
    {
    image<> img1; // Default 5x5 array initialized to 0
    image<6,2> img2(5); // Custom 6x2 array initialized to 5

    // Show some stats for img1
    cout << "img1 - sizeof: " << sizeof(img1)
    << " height: " << img1.Height()
    << " width: " << img1.Width()
    << endl;
    [blue]cout << img1;[/blue]

    // Show some stats for img2
    cout << "img2 - sizeof: " << sizeof(img2)
    << " height: " << img2.Height()
    << " width: " << img2.Width()
    << endl;
    [blue]cout << img2;[/blue]

    return 0;
    }
    [/code]

    Looks much better, much more clean looking this way. The output should be the same as already described above.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories