0

I am having problem with passing a pointer by reference.

This is the method

void set_range(double **measu)  
{   
if ((*measu)[0] < 0) //or i is used for a loop  
   return ;         
}


int main()
{
   double *mes;
   set_range(&mes[1]);

}

I have allocated memory and required values are set. But this program gives me "Unhandled exception Access violation reading location" error. So my question is,how to pass the pointer of mes[1] instead of mes[0] (which normally passed when (&mes) is given) to the set_range method?

1
  • 1
    It ought to give you a few more errors. You should crank up your compiler warnings. Commented Dec 27, 2010 at 4:06

2 Answers 2

3

One problem is that &mes[1] is of type double *, not the double ** required of your function.

Another problem is that mes doesn't point to anything - it's uninitialized. So dereferencing it will access junk (which is why you get an access violation).

I'm trying to come up with some code to help clarify, but honestly I have no idea what you're trying to do. Some more code would help us figure out what your goal is, but just given the above I have no idea why you need a double ** or whether you need dynamic memory or just a single double variable.

Sign up to request clarification or add additional context in comments.

Comments

1

Change your function to take a double* instead of a double**, eg:

void set_range(double *measu)
{
  if (*measu < 0) //or i is used for a loop
    return;
}

int main()
{
  double *mes;
  ...
  set_range(&mes[1]);
}

Alternatively, use a real reference instead:

void set_range(double &measu)
{
  if (measu < 0) //or i is used for a loop
    return;
}

int main()
{
  double *mes;
  ...
  set_range(mes[1]);
} 

2 Comments

This question isn't tagged C++ - there are no references in C.
I changed it to C++. Remy has given what i am expecting (how to pass the pointer of mes[1] instead of mes[0])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.