Home Forums C Programming pointer of array of class

Viewing 1 reply thread
  • Author
    Posts
    • #2089
      harleen
      Participant

      hello!
      i have problem with program i made:
      the program should define a pointer to pointer which points to dynamic array of Grade (class i made).
      i increase the size of the array in function. so i made a new pointer to pointer which point to array of Grade (the class i made). i transferred the old array to new one, and finaly, before i going out of the function, i shoud transfer it’s address to the first pointer i made:

      
      
      {
      int students=1;
       
       Grade ** numGrades=new Grade * [students];
       numGrades[0]=new Grade;
       assert(numGrades!=0);
       AddGrade(numGrades,students); 
      }
      void AddGrade(Grade ** _numGrades,int& _students)
      {
       _students++;
       Grade ** gradeTemp=new Grade * [_students];
       for(int i=0;i<_students-1;i++)
        gradeTemp=_numGrades;
       gradeTemp=new Grade;
      }

      how can i transfer the address of “gradeTemp” to “numGrades”?
      thank you,
      Moshiko

    • #3375
      Humayan
      Participant

      You want to return the newly copied array from the function “AddGrade” ?  If so change your parameters to :
       

      void AddGrade ( Grade **&  _numGrades , int &  _students )
      {
         Grade ** gradeTemp = new Grade * [ _students + 1 ];
       
         for( int i = 0 ; i < _students + 1 ; i ++ )
             gradeTemp [ i ] = _numGrades [ i ] ;
         _students ++;
        //add code here to delete old "_numGrades" 
        _numGrades = gradeTemp;
      }
Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.