Please help with a small question

  • Thread starter Thread starter Gena
  • Start date Start date
G

Gena

Hi ,

I'm a newbe to programming and have a small question:

I made a small program with a class. in the class's header file I have
: double *ptr_output;

Void main()
{
double result[4];

class class_name(4); //constructor

class.method(result);
}

class.method(double *output)
{
memcpy(output,ptr_output,4);
}

class constructor
{
ptr_output = new double[length];
}


Why doesn't this work ? I want the method to peform some work on the
data and then return it to the result array.

Please help
 
Gena said:
Hi ,

I'm a newbe to programming and have a small question:

I made a small program with a class. in the class's header file I have
: double *ptr_output;

Void main()
{
double result[4];

class class_name(4); //constructor

class.method(result);
}

class.method(double *output)
{
memcpy(output,ptr_output,4);

Apart from numerous syntax errors, the most obvious problem is the line
above. It should be:

memcpy(output,ptr_output,4 * sizeof ptr_output[0]);

memcpy takes a byte count, but doubles are bigger than 1 byte, so you
need to multiply up by sizeof(double). I've used sizeof ptr_output[0] in
case you ever change the type of ptr_output (e.g. to long double or float).

Why didn't you copy-and-paste your real code? Surely that's quicker than
typing it out again?

Tom
 
Back
Top