Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: how to declare 2D array?

  1. #1

    A 2D array is the same as double pointer, right?

    I have two questions about arrays.

    1) I created an array with [][] and passed it into a function expecting **. I got an error saying: cannot convert parameter 1 from 'gfx::Vec3f [800][600]' to 'gfx::Vec3f **. I thought [][] and ** are the same thing!

    2) I tried doing "Vec3f image[width][height];" before, but the compiler complained saying "expected constant expression". So I changed it to "Vec3f image[800][600];" to make it compile. Why must I do this hardcoding?

    Code:
    Pixels::Pixels(Vec3f** colors, int width, int height)
    {
    	this->width = width;
    	this->height = height;
    	this->image = colors;
    }
    Code:
    int width = 800;
    int height = 600;
    
    Vec3f image[800][600];
    
    for(int w = 0; w < width; w++)
    	for(int h = 0; h < height; h++)
    		image[w][h] = Vec3f(w / width, h / height, w / height); 
    
    Pixels pixels = new Pixels(image, width, height);
    Last edited by NeoDreamer; September 4th, 2008 at 12:34 AM.

  2. #2
    jwilliam's Avatar
    476
    posts
    An intellectual carrot!
    Don't you have to dereference 'image' to get its memory address if the function is expecting a pointer?

    Code:
    Pixels pixels = new Pixels(&image, width, height);
    It's been a while, so I may just be blowing smoke.

  3. #3
    1) [][] is a const pointer to a const pointer. Try making your ** const.
    2) expressions within [] need to be constant and determined at compile time. Using a variable inside [] is not good enough since the variable can change. If it was a const variable declared and defined, then it's fine.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  4. #4
    I solved the array size issue, but my call to the constructor is still not working

    Code:
    const int width = 800;
    const int height = 600;
    
    Vec3f image[width][height];
    
    for(int w = 0; w < width; w++)
    	for(int h = 0; h < height; h++)
    		image[w][h] = Vec3f(w / width, h / height, w / height); 
    
    Pixels pixels = new Pixels(&image, width, height);
    I've tried the above code with:

    Code:
    Pixels::Pixels(Vec3f** colors, int width, int height)
    
    // and this:
    
    Pixels::Pixels(const Vec3f** colors, int width, int height)
    I get the same conversion error.

  5. #5
    Try Vec3f const * const * colors.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  6. #6
    Man, nothing works. I'm switching to Java - the easy way out

  7. #7
    try Vec3f* colors[]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012