passing a reference to an array?

This is a discussion on passing a reference to an array? within the C++ Programming forums, part of the General Programming Boards category; I won't post all my code, as it is repetetive and long, but here is the class definition my question ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    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:

    Code:
    class cellField
    {
      public:
      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();
    
      private:
      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:

    http://dydx.no-ip.com:8080/cellauto.cpp

    (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!

    Thanks!

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    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!
    "SHUT YOUR LIPS..."

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    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
    Posts
    244
    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.

    thanks!

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

    http://dydx.no-ip.com:8080/cellauto2.cpp

    Any more tips?

    Thanks!

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

    Code:
     
    
    //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, 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