Home › Forums › C Programming › pointer of array of class
- This topic has 1 reply, 2 voices, and was last updated 16 years, 8 months ago by Humayan.
Viewing 1 reply thread
- AuthorPosts
- April 5, 2008 at 6:20 am #2089harleenParticipant
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:1234567891011121314151617181920<hr class="bbcode_rule" />{<br />int students=1;<br /><br />Grade ** numGrades=new Grade * [students];<br />numGrades[0]=new Grade;<br />assert(numGrades!=0);<br />AddGrade(numGrades,students); <br />}<br />void AddGrade(Grade ** _numGrades,int& _students)<br />{<br />_students++;<br />Grade ** gradeTemp=new Grade * [_students];<br />for(int i=0;i<_students-1;i++)<br />gradeTemp=_numGrades;<br />gradeTemp=new Grade;<br />}<hr class="bbcode_rule" />how can i transfer the address of “gradeTemp” to “numGrades”?
thank you,
Moshiko - April 5, 2008 at 1:05 pm #3375HumayanParticipant
You want to return the newly copied array from the function “AddGrade” ? If so change your parameters to :
12345678910void AddGrade ( Grade **& _numGrades , int & _students )<br />{<br />Grade ** gradeTemp = new Grade * [ _students + 1 ];<br /><br />for( int i = 0 ; i < _students + 1 ; i ++ )<br />gradeTemp [ i ] = _numGrades [ i ] ;<br />_students ++;<br />//add code here to delete old "_numGrades" <br />_numGrades = gradeTemp;<br />}
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.