Click here to Skip to main content
15,887,856 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!


C#
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);
}
Posted

You do not need a double pointer for this, you can simply pass the single pointer around like so:

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);
}
 
Share this answer
 
Comments
Olivier Levrey 10-Mar-11 4:34am    
My 5.
Donald Hume 10-Mar-11 9:40am    
Thaddeus Jones, you're the man. I had to change the function call of setAngle to the following though:

setAngle(x,y,&angle);

I think this makes sense though right?
[no name] 10-Mar-11 11:05am    
It depends; in this example, we call setAngle with angle rather than &angle, because it's called from within doStuff1. doStuff1 knows angle as a pointer to double, so this is correct.
If you were to call setAngle from main() though, or any other place where angle is known as a double rather than a pointer to double, it would indeed be &angle.
Donald Hume 10-Mar-11 14:08pm    
You are most certainly correct. Thanks so much for your help!
[no name] 10-Mar-11 14:11pm    
You're welcome :)
C#
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
}

 
Share this answer
 
Hi,
try this
void main()
{    
     double angle;    
     doStuff1( ... , &angle);    
     doStuff2( ... , &angle);
}

if working then i will explaination
 
Share this answer
 
Comments
Olivier Levrey 10-Mar-11 4:35am    
You could have explained right now. Anyway, my 5.
[no name] 17-Mar-11 2:39am    
function uotside the class doesn't take right parameter mostly took a garbage value becouse this have memory assignment or some thing else.
In your method, value of angle has not been modified by setAngle. setAngle simply gets an unitialized pointer.
I am assuming you want the called function to return an address to the caller.
Your functions should take an argument a pointer to a pointer.
setAngle(double v1[3], double v2[3], double **angle)
{
}
in main, you need to do this:
void main()
{
double *angle;
doStuff (..., &angle);
// pointer angle is now initialized and is pointing to the right address
}
 
Share this answer
 
Comments
Michael Melkonian 9-Mar-11 22:55pm    
Meant to be setAngle, not doStuff in main.
e.g.
void main()
{
double *angle;
setAngle(..., &angle); // pointer is now initialized...
}
[no name] 9-Mar-11 22:59pm    
That what i mentioned above
actauly when pointer intialise it take garbage value but when passed value pointer it takes define value
Michael Melkonian 9-Mar-11 23:17pm    
I was under impression that he wants setAngle to return a pointer to a double, not a double value. For a value the answer is of course trivial (like you said).
scu_sundy 9-Mar-11 23:59pm    
Here is the right way.
Harrison H 10-Mar-11 13:09pm    
If you go that route, you'll set some garbage space in memory to whatever setAngle tells it too. You're assigning the angle value to an unitialized pointer. Bad Bad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900