使用指针引用传递特定数组元素

发布于 2024-10-09 10:08:00 字数 361 浏览 0 评论 0原文

我在通过引用传递指针时遇到问题。

方法

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]);

}

这是我分配内存并设置所需值的 。但是这个程序给了我“未处理的异常访问冲突读取位置”错误。 所以我的问题是,如何将 mes[1] 而不是 mes[0] 的指针(通常在给出 (&mes) 时传递)传递给 set_range 方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-10-16 10:08:00

一个问题是 &mes[1] 的类型为 double *,而不是函数所需的 double ** 类型。

另一个问题是 mes 没有指向任何东西 - 它没有初始化。因此取消引用它将访问垃圾(这就是您遇到访问冲突的原因)。

我试图想出一些代码来帮助澄清,但老实说我不知道​​你想做什么。更多代码可以帮助我们弄清楚您的目标是什么,但仅考虑到上面的内容,我不知道为什么您需要一个 double ** 或者您是否需要动态内存或只需要一个 double变量。

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.

静赏你的温柔 2024-10-16 10:08:00

更改您的函数以采用 double* 而不是 double**,例如:

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

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

或者,使用真实引用:

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

int main()
{
  double *mes;
  ...
  set_range(mes[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]);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文