Passing Array by Reference...

This is a discussion on Passing Array by Reference... within the C++ Programming forums, part of the General Programming Boards category; So I'm making a game (this isn't a game-related question, though)... This may sound simple, but I haven't been able ...

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Passing Array by Reference...

    So I'm making a game (this isn't a game-related
    question, though)... This may sound simple, but
    I haven't been able to figure out how to do this.

    I have a function that reads the information
    from a bitmap, minus the palette. I'm trying to
    write a function that would read only the
    palette-information, and store it in a global-
    array that is passed through the function. This
    is what I tried (I'm still new), but it didn't
    work...

    Code:
    BYTE g_GamePalette[256][3];
    
    class Graphic{
     public:
      ReadImageInfo(char *name);
      ReadPalette(char *name, BYTE &palette[256][3]);
      ...
     private:
      ...
    };
    
    int main(){
      Graphic *image = new Graphic;
      image->ReadBitmapInfo{"bitmap.bmp");
      image->ReadPalette("bitmap.bmp", g_GamePalette[256][3]);
    
      /*********************************************
      *How would I pass the entire global array, by*
      *reference, though the ReadPalette() functio?*
      *********************************************/
    
      return 0;
    }

    Project: 13h Game.
    Compiler: DJGPP.
    Operating System: Windows 95.

    Thanks a lot,
    ethic.
    Staying away from General.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Try;

    Code:
    ReadPalette(char *name, BYTE palette[][3])
    and

    Code:
    image->ReadPalette("bitmap.bmp", g_GamePalette)

    I think that works, but havent tested it.

    Also, if the array is global, why are you passing it with the function?

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    arrays are always passed by reference anyway. When you pass an array in reality you are passing a constant pointer to the arrays first element.
    ReadPalette(char *name, BYTE &palette[256][3]);
    should be
    ReadPalette(char *name, BYTE palette[256][3]);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    >>Also, if the array is global, why are you passing it with the function?

    I kind of want to make the class usable by other
    people. If I do this, then they can name their
    global palette anything they want... I think...
    People will probably never use it, but doing
    things this way may help me learn how to design
    great graphic-engines (far into the future).

    Also, I would be able to create multiple palettes
    without having to write a seperate function for
    each. This is good, too...
    Last edited by Cheeze-It; 02-16-2002 at 12:37 PM.
    Staying away from General.

  5. #5
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    Sorry to bump this, but I'm still having trouble.

    First I tried this, but it wouldn't compile. I
    kept getting a error that said there wasn't a
    matching function inside the GRAPHIC class (if
    I understood it correctly)...

    Code:
    class GRAPHIC{
     public:
       bool ReadImageInfo(char *BitmapName);
       bool ReadPalette(char *BitmapName, BYTE Palette[256][3]);
     private:
      ...
    };
    
    BYTE MainGamePalette[256][3];
    
    int main(){
      GRAPHIC *image = new GRAPHIC;
      image->ReadImageInfo("Bitmap.bmp");
      image->ReadPalette("Bitmap.bmp", MainGamePalette[256][3]);
    
      return 0;
    }
    Then I tried this (the next example). This compiled,
    but wouldn't run. As soon as I tried to run the
    game, I got a whole bunch of hex-things that I
    don't understand, yet. It said something like:
    "Exiting due to Signal SIGSEGV"...

    I don't know if this error is related to my
    palette-problem, or not. But it didn't happen
    until after I changed my code (I'm still looking
    at things)...

    Code:
    BYTE MainGamePalette[256][3];
    int main(){
      ...
      image->ReadPalette("Bitmap.bmp", MainGamePalette);
    
     return 0;
    
    }
    Can anybody help? Is the second example correct
    (because it compiles), and my problem lies
    elswhere? Or is it completely wrong?

    Thanks a lot,
    ethic.
    Staying away from General.

  6. #6
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    Okay, I'm dumb. Nevermind; I think the second
    example is correct. I just got the thing to run.
    Apparently when I commented-out the palette-reading
    part of the Bitmap-reading function, I forgot
    to skip the bitmap's palette-information.

    Sorry.
    Staying away from General.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 04:19 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21