Thread: pointer of array of class

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    pointer of array of class

    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:
    Code:
    {
    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[i]=_numGrades[i];
    	gradeTemp[i]=new Grade;
    
    }
    how can i transfer the address of "gradeTemp" to "numGrades"?

    thank you,
    Moshiko

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The same way you did for _students, use a reference.

    BTW, symbols which begin with an underscore are reserved identifiers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User scarfish's Avatar
    Join Date
    Apr 2008
    Posts
    3
    or just call AddGrade with the adress of numGrades and use it in AddGrade as a pointer of a pointer of a pointer^^

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Code:
    	AddGrade(numGrades,students);	
    
    void AddGrade(Grade ** &_numGrades,int& _students)
    {
    .
    .
    .
    }
    but i can't define it like this, it write an error. so how can i define _numGrade as a reference of numGrades?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could start with posting your error message.

    Did you also fix the prototype to reflect that change?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would rather call it pass by pointer than pass by reference. Pass by reference is what we do in C++.
    A nitpick detail, I know, but it's more correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM