c++ - Set new size of dynamic array from an external function/class? -
my main code declares pointer needs resized external function. ex:
double *vect =null; classobj object; object.func(vect); //--- print outcome for(int j=0; j<4; ++j) cout << vect[j] << " .. " << endl;
where function func() part of classobj
defined in file (say classobj.h
, .cpp
) as
void classobj::func(double *_vect){ _vect = new double[4]; for(int j=0; j<4; ++j) _vect[j] = 3.0*j +1 ; };
the problem vect
has not been resized. segmentation fault. idea please on how use pointers in case?
you passing reference array , creating new array , assigning pointer in function. need pass pointer of pointer , reassign address of pointer:
void classobj::func(double **_vect){ (*_vect) = new double[4];
although solution work recommend using std::vector
purpose , passing reference function
Comments
Post a Comment