Arrays of references

This is a discussion on Arrays of references within the C++ Programming forums, part of the General Programming Boards category; Still making this minesweeper clone, hehe. Anyway, i cant make an array of references apparantly, i googled it and came ...

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Arrays of references

    Still making this minesweeper clone, hehe. Anyway, i cant make an array of references apparantly, i googled it and came out with this:
    Operators are not applied to references but to the variable of which it is a synonym. For instance, we cannot know the address of a reference, only of its synonym; we cannot define a pointer to a reference, it would point to its synonym; we cannot define an array of references, it is an array of the synonyms; etc.
    ...of which i have no idea what it's talking about, lol. Now this is refering to my minefield, which i wanted to create as a 9x9 2d array, but when i pass it to my random mine placing function im a little stuck. A friend just suggested using a pointer, but surely that would generate basically the same error? Is there any way that i could do this? other than using a global variable
    Last edited by Welshy; 07-02-2005 at 12:22 PM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Still making this minesweeper clone, hehe. Anyway, i cant make an array of references apparantly, i googled it and came out with this:
    There's no null reference in C++ so all references must set to an object at creation, which means the only way you can do have an int& A[5] is if you put initial values in the array.

    Operators are not applied to references but to the variable of which it is a synonym. For instance, we cannot know the address of a reference, only of its synonym; we cannot define a pointer to a reference, it would point to its synonym; we cannot define an array of references, it is an array of the synonyms; etc.
    I don't know what this quote is saying either. References are like pointers that are always dereferenced and always referencing an object. Hence, when you take the address of a reference, you retrieve a pointer with value to what the reference references Maybe the quotation is making a distinction that the reference doesn't have its own unique pointer

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    An array can be viewed as a pointer to its first element. For a 2d array, that element just happens to be an array too.

    So if you're passing your array to a function, chances are you're passing it as a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, to your comment of not understanding, think of a reference as being similar to a pointer. When someone talks of a reference's synonym, he or she is talking about what the reference refers to. If you think of it like a pointer, a synonym is like what the pointer is pointing to.

    But, references are NOT pointers. A reference is a construction that makes it easier to modify variables in a function that are not local to the function. If you ever worked with C, I'm sure you came across this, but if not, I'll lay it out for you. It's pointless, but let's say you were trying to write a function that adds one to a variable and returns nothing. In C, you would do this...
    Code:
    void increment(int* value)
     {
      *value += 1;
     }
    This way, the variable you send to the function would be incremented, but you'd always have to remember to slap an address label (&) onto the variable when you passed it to the function. In C++, you would use a reference.
    Code:
    void increment(int& value)
     {
      value += 1;
     }
    You cannot operate on a reference. A reference is designed to be a 'middle man' for operations that are to be performed on another variable.

    So, to your question about whether you would get the same errors, not really. You can have an array of pointers passed to a function. It's perfectly legal. Just set your program up so that each element of that array of pointers points to a mine location.
    Code:
    void function(void)
     {
      function();
     }

  5. #5
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I cant put an initial values in the array as it's an array of a class. So if i were to use pointers, would i use
    Code:
    Class* pName[9][9]
    or is there a special way?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Just use
    Classname pName[9][9];
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by laserlight
    Just use
    Classname pName[9][9];
    But isnt that going to just make a copy of it and not alter the original array?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    But isnt that going to just make a copy of it and not alter the original array?
    Try and see for yourself. You will find that you are actually dealing with pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I cant put an initial values in the array as it's an array of a class. So if i were to use pointers, would i use
    No, you can do have arrays of class references, but you must either use default constructors in the class or explicitly construct the objects. For example, if I have

    Code:
    class C {
          C(int a) { }
    };
    I must do
    Code:
    C& array[4] = {C(5), C(3), C(2), C(6) };
    to declare an array of references to C.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    I dont think that is right, okinrus. One cannot declare a reference to a temporary, and certainly not an array of references to temporaries.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I dont think that is right, okinrus. One cannot declare a reference to a temporary, and certainly not an array of references to temporaries.
    Yes, I think you're right. I remember trying something like it along time ago and getting it to work. But I didn't have any real use for it, though. Now, I'm not getting it to work on my compiler, so I must have been either not using references or had the variables declared global.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Here's a pretty good explanation of references. I guess an array of references is impossible in C++. I must have confused a simple array with an array of references. http://cpptips.hyperformix.com/cpptips/array_ref4

  13. #13
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    If you are having problems using classes then maybe you should think about not using them. Re design your plan perhaps?

    It seems to me that you can do this with functions alone.


    Code:
    /*Minesweeper BS
      Create a 9x9 2D array
      and fill randomly fill with mines.
      
      Eg. Fill array with five mines
          A '*' represents a mine
          A '.' represents an empty location
       
       1  2  3  4  5  6  7  8  9
     1 [.][.][.][.][.][.][.][.][.]
     2[*][.][.][.][.][.][.][.][.]
     3 [.][.][.][*][.][.][.][.][.]
     4 [.][.][.][.][.][.][.][.][.]
     5 [.][.][.][*][*][.][.][.][.]
        .
        . .
        .  .
     9 [.][*][.][.][.][.][.][.][.]
    
    */
    
    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctime>
    #include <iostream>
    #include <conio.h>
    
    int GetRand(int min, int max);
    
    using namespace std;
    
    int main()
    {
       char array[10][10];
       int i, r, N,n,nt;
       int x[80];
       int y[80];
       
       cout<<"Randomly placing 5 mines..."<<endl;
       N=5;
       
       for (int i=0; i<N; i++)
       {
            n = GetRand(0,8);
       
           x[i]=n;
           //cout<<" "<<x[i];
           
       }
       cout<<endl;
       for (int j=0; j<N; j++)
       {
            nt = GetRand(0,8);
            
            y[j]=nt;
       }
       cout<<endl;
      
      //initialise array
      for (int a=0; a<9; a++)
      {
          for (int b=0; b<9; b++)
          {
              array[a][b]=' ';
          }
      }        
           
       
       
       
          
      
      for (int i=0; i<9; i++)
      {
          for(int j=0; j<9; j++)
          {
              for (int k=0; k<N; k++)
              {
                if((i==x[k])&&(j==y[k]))
                {
                    
                    array[i][j]='*';
                }
                
              }    
          }
      }                
      
      
      
      
      for (int i=0; i <9; i++)
      {
          for (int j=0; j<9; j++)
          {
              cout<<"["<<array[i][j]<<"]";
          }
          cout<<endl;
      }        
     
      
    
     getch();
      
      return 0;
    }
    
    
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int r;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      r = (rand() % (max - min + 1) + min);
      
      return r;
    }
    Sample output:

    Code:
    Randomly placing 5 mines...
    
    
    [ ][ ][ ][ ][*][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][*]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][*][ ][ ][*][ ]
    [ ][ ][ ][ ][ ][*][ ][ ][ ]
    (Nb: The place mine function has a major bug, it doesn't check
    for repeats!)

  14. #14
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Good idea Treenef, except im using a cell class, with things like whether it's flagged, contains a mine, how many mines around it etc...so i'd really like to use OOP with this, and dont really see functions being able to fulfil what i'd like it to do.

    I'll try Laserlights idea and get back to you if it doesnt work, just keep an eye out for more threads started by me, hehe

  15. #15
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Here's the second part using just functions. The code's a bit ugly though.
    Code:
    /*Minesweeper BS
    */
    
    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctime>
    #include <iostream>
    
    
    
    int GetRand(int min, int max);
    void count_mines_around(char[][10],int x[],int y[],int N);
    
    
    using namespace std;
    
    int main()
    {
       char array[10][10];
       int i, r, N,n,nt;
       int x[80];
       int y[80];
       
       cout<<"Randomly placing 5 mines..."<<endl;
       N=5;
       
       for (int i=0; i<N; i++)
       {
            n = GetRand(0,8);
       
           x[i]=n;
           //cout<<" "<<x[i];
           
       }
       cout<<endl;
       for (int j=0; j<N; j++)
       {
            nt = GetRand(0,8);
            
            y[j]=nt;
       }
       cout<<endl;
      
      //initialise array
      for (int a=0; a<9; a++)
      {
          for (int b=0; b<9; b++)
          {
              array[a][b]=' ';
          }
      }        
           
       
       
       
          
      
      for (int i=0; i<9; i++)
      {
          for(int j=0; j<9; j++)
          {
              for (int k=0; k<N; k++)
              {
                if((i==x[k])&&(j==y[k]))
                {
                    
                    array[i][j]='*';
                }
                
              }    
          }
      }                
      
      
      
      
      for (int i=0; i <9; i++)
      {
          for (int j=0; j<9; j++)
          {
              cout<<"["<<array[i][j]<<"]";
          }
          cout<<endl;
      }        
     
      count_mines_around(array,x,y,N);
    
      int stop;
      cin>>stop;
      
      return 0;
    }
    
    
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int r;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      r = (rand() % (max - min + 1) + min);
      
      return r;
    }
    
    
    
    void count_mines_around(char array[10][10],int x[10],int y[10],int N)
    {
       char array_t[10][10];
       
        
        for (int i=0; i<9 ; i++)
        {
            for (int j=0; j<9; j++)
            {
                int i_n=i-1;
                int j_n=j-1;
                
                int counts=0;
                int counter=0;
                for (int y=i_n; y<i_n+3; y++)
                {
                    for(int z=j_n; z<j_n+3; z++)
                    {
                        counter++;
                        if ((y>=0)&& (y<=8))
                        {
                            if ((z>=0)&& (z<=8))
                            {
                             if (counter!=5)
                             {
                              if(array[y][z]=='*')
                              {
                               counts++;
                               
                              } 
                              } 
                                 
                              
                            }
                         }           
                    }
                    
                }
                    counter=0;
                    //cout<<counts;
                    
                   if (counts==0)
                   {
                       array_t[i][j]=' ';
                   }   
                   if(counts==1)
                   {
                       array_t[i][j]='1';
                   }
                   if(counts==2)
                   {
                       array_t[i][j]='2';
                   }
                   if(counts==3)
                   {
                       array_t[i][j]='3';
                   }
                   if(counts==4)
                   {
                       array_t[i][j]='4';
                   }
                   if(counts==5)
                   {
                       array_t[i][j]='5';
                   }
                   if(counts==6)
                   {
                       array_t[i][j]='5';
                   }
                   if(counts==7)
                   {
                       array_t[i][j]='7';
                   }
                   if(counts==8)
                   {
                       array_t[i][j]='8';
                   }
                   if(counts==9)
                   {
                       array_t[i][j]='9';
                   }
                        
      
                   
                    counts=0;
            }
            
        }
    
    cout<<""<<endl;
    cout<<""<<endl;
    
    
    
      for (int i=0; i <9; i++)
      {
          for (int j=0; j<9; j++)
          {
              for (int k=0; k<N; k++)
              {
                if((i==x[k])&&(j==y[k]))
                {
                    
                    array_t[i][j]='*';
                }
                
              }    
              cout<<"["<<array_t[i][j]<<"]";
          }
          cout<<endl;
      } 
    
     }

    Sample output:
    Code:
    Randomly placing 5 mines...
    
    
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][*]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][*]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][*][ ][ ][*][ ]
    [ ][ ][ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][*][ ][ ][ ][ ][ ][ ]
    
    
    [ ][ ][ ][ ][ ][ ][ ][1][1]
    [ ][ ][ ][ ][ ][ ][ ][1][*]
    [ ][ ][ ][ ][ ][ ][ ][1][1]
    [ ][ ][ ][ ][ ][ ][ ][1][1]
    [ ][ ][ ][ ][ ][ ][ ][1][*]
    [ ][ ][ ][1][1][1][1][2][2]
    [ ][ ][ ][1][*][1][1][*][1]
    [ ][1][1][2][1][1][1][1][1]
    [ ][1][*][1][ ][ ][ ][ ][ ]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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