本文介绍了无法从函数来回传递指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在编写一些代码,并且一直在使用3D数组和指针做一些事情.但是现在突然之间,我只需要传递一个值指针,我就在挣扎(尴尬,我知道).我已经读完了指针tut,但我仍在努力.以下是我要尝试的操作,但我只是遇到类似于无法分配double = * double"之类的错误.如果有人能给我一个简单的例子,我一定会很感激的.
非常感谢!


Hey guys,
I am working on some code, and I''ve been doing some stuff with 3D arrays and pointers. But now all of a sudden I just need to pass a single value pointer and I''m struggling (embarrassing, I know). I''ve read over the pointer tut but I am still struggling. Below is what I am trying to do, but I''m just getting errors similar to "cannot assign double = *double" or the like. If someone could give me a simple example I would definitely appreciate it.
Thanks so much!


void setAngle (double v1[3], double v2[3], double *angle) {
  // set the value of angle
}
void doStuff1 ( ... , double *angle) {
    setAngle(x, y, angle);
}
void doStuff2 ( ... , double *angle) {
    // use angle here
}
void main() {
    double * angle;
    doStuff1( ... , angle);
    doStuff2( ... , angle);
}

推荐答案

void setAngle (double v1[3], double v2[3], double *angle)
{
    // Assign some value to the angle
    *angle = 90.0;
}

void doStuff1 ( ... , double *angle)
{
    setAngle(x, y, angle);
}

void doStuff2 ( ... , double *angle)
{
    // Do something with angle
    printf("%.1f",*angle);
}

void main()
{
    double angle;
    doStuff1( ... , &angle);
    doStuff2( ... , &angle);
}


double some_data[] =
{
    0.0,
    0.1,
    0.2
};

void setAngleCorrect (double **angle)
{
    *angle = &some_data[0];
}

void setAngleIncorrect (double *angle)
{
    angle = &some_data[0];
}

void main()
{
    double *angle1;

    setAngleCorrect(&angle1);

    ASSERT (angle1 == some_data); // this works

    double *angle2;

    setAngleIncorrect(angle2); // this is an error, pointer is uninitialized

    ASSERT (angle2 == some_data); // the two addrresses are not equal
}




void main()
{    
     double angle;    
     doStuff1( ... , &angle);    
     doStuff2( ... , &angle);
}


如果可以的话我会解释


if working then i will explaination


这篇关于无法从函数来回传递指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:50