使用指针引用传递特定数组元素
我在通过引用传递指针时遇到问题。
方法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个问题是
&mes[1]
的类型为double *
,而不是函数所需的double **
类型。另一个问题是
mes
没有指向任何东西 - 它没有初始化。因此取消引用它将访问垃圾(这就是您遇到访问冲突的原因)。我试图想出一些代码来帮助澄清,但老实说我不知道你想做什么。更多代码可以帮助我们弄清楚您的目标是什么,但仅考虑到上面的内容,我不知道为什么您需要一个
double **
或者您是否需要动态内存或只需要一个double变量。
One problem is that
&mes[1]
is of typedouble *
, not thedouble **
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 singledouble
variable.更改您的函数以采用
double*
而不是double**
,例如:或者,使用真实引用:
Change your function to take a
double*
instead of adouble**
, eg:Alternatively, use a real reference instead: