Thread: passing a reference to an array?

  1. #1
    Registered User
    Join Date
    Aug 2001

    passing a reference to an array?

    I won't post all my code, as it is repetetive and long, but here is the class definition my question concerns:

    class cellField
      void generateField(char[10][10]); // randomly generates the starting field
      void generateField();
      void advanceTime();   // advances the time and applies cell rules
      void advanceTime(char [10][10]);
      void displayField(char[10][10]);
      void displayField();
      char fieldArray[10][10];
      char tempField[10][10];
    The program creates a 10x10 grid of X's, O's, and I's, and displays them. Then it is supposed to advance the "time", and certain rules are supposed to change the "field". (a basic cellular automation system).

    I won't explain the whole thing, but generateField and displayField and advanceTime need to accept a reference to a 10x10 character array. But if I put "void advanceTime(char &[10][10];", it says that the function is being implemented twice. But they have different inputs, so why is that happening? Can I not do this, or do I need to do it another way?

    Here is the code in case anyone needs to look at it:

    (there may be a couple parts of it that seem "broken", but thats probably just because I stopped while trying to fix it and said "BLAH!" and came here)

    And yea, the program is sort of useless, but I like doing little "excercises" like this to give myself practice.

    Anyways, help!


  2. #2
    Isn't an array already a reference?

    What happens when you try to just put the array as the function arg??

    Also, putting an array as a function argument just seems wrong to me, don't know why it just strikes me that way.

    Maybe try to take those arrays in the function args and make it one Public or Private member variable. Then have your functions use that member var inside the function instead of trying to use it in the ags list.

    Get rid of the '&' ampersand and give that array a name, it's okay to just say char[10][10] in the class declaration but in the implementation it should be char MyChar[10][10] or something similar.

    If you must have an array as an arg in your functions maybe you can use a pointer to it instead. Need some help on this as I'm a bit rusty but I know you can create an array of pointers but not sure if it's legal to create a ptr to an array??
    My Avatar says: "Stay in School"

    Rocco is the Boy!

  3. #3
    Registered User
    Join Date
    Oct 2002
    An array is a reference. Example...
    int myArray [10];

    myArray == &myArray[0]

    Need to think of arrays and pointers as the same thing since aside from a slight mundane detail they are the same exact thing.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Hmm I did originally try passing them as just arrays, but after the first time I advanced "time", the field kept staying the same.

    I think that must have been another problem...

    Hmm.. I guess I'm just beeing foolish. When I get a chance I'll have to think the advanceTime function through again.


  5. #5
    Registered User
    Join Date
    Aug 2001
    Ok here it is, new and improved (i re-thought my methods - I don't know if its better or not, but it works):

    Any more tips?


  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    It can be done.

    //10x10 array:
    char arr[10][10];
    //reference to 10x10 array
    char (&rarr)[10][10];
    //Function prototype
    void advanceTime(char (&)[10][10]);
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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, 05: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