passing arrays into functions by reference

This is a discussion on passing arrays into functions by reference within the C++ Programming forums, part of the General Programming Boards category; for some reason, i can't figure out how to pass an array into a function properly. here's the code where ...

  1. #1
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301

    passing arrays into functions by reference

    for some reason, i can't figure out how to pass an array into a function properly.

    here's the code where i'm encountering the error:

    function declaration:
    Code:
    void initMapBuffer(map map1, CHAR_INFO &mapBuffer[]);
    function body:
    Code:
    void initMapBuffer(map map1, CHAR_INFO &mapBuffer[])
    {
     for (int y = 0; y < map1.max.Y; y++)
     {
      for (int x = 0; x < map1.max.X; x++)
      {
       mapBuffer[x + y * MAP_BUFFER_W].Char.AsciiChar = map1.tiles[x][y].symbol;
      }
     }
    }
    the map struct:

    Code:
    struct map
    {
     tile tiles[MAP_MAX_X][MAP_MAX_Y];
     COORD max;
    };
    the tile struct
    Code:
    struct tile
    {
     char symbol;
     char foreground, background;
    };

    please answer this question. I usually can figure these things out myself, but i'm stumped on this one. This piece of code is the cornerstone for my RPG, without it, i can't load maps and display them to the screen.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    CHAR_INFO &mapBuffer[]

    is an array of references, which isn't legal C++. You could try something like

    CHAR_INFO (&mapBuffer)[SIZE]

    if you're willing to commit a size to the arrays that'll be passed into the function, but as arrays are really passed by address they're passed by reference anyway so I see no reason not to just do

    CHAR_INFO mapBuffer[]

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    oh.. i thought that you passed arrays by value if you didn't use the address of (&) operator. Alright, i'll try it out.

  4. #4
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    alright, now i'm getting an illegal operation when i execute it.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    By default array's are passed by reference. All an array is, is a pointer to the first element in a series.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    CHAR_INFO &mapBuffer[])
    What excactly is CHAR_INFO?

    Another thing you might try, silly, but, only cycle through half the array and see if it still crashes...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    i'm using buffers to display the map for my roguelike to the screen, an array of CHAR_INFO stores what charactars and their attributes will go into each character slot in the buffer.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Originally posted by Aran Elus
    oh.. i thought that you passed arrays by value if you didn't use the address of (&) operator. Alright, i'll try it out.
    When you pass array's to a function, all you are really passing is the address of the first element. The compiler lets you use array notation ( arrayname[int] ) to make coding easier, If you change a value in the array in the function, the array from the calling function will be modifed as well.
    So try this

    void functionName( type * arrayname)

    This will be ok if you know how big the array is going to be, or you could pass a size paramater along with the array name

    void functionName( type * arrayname, int size)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arrays to Functions...
    By txcs in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 10:36 AM
  2. Functions which return a reference
    By Stonehambey in forum C++ Programming
    Replies: 10
    Last Post: 07-05-2008, 01:43 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

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