Click here to Skip to main content
14,102,245 members
Rate this:
 
Please Sign up or sign in to 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!


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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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);
}
   
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?
User 7429338 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!
User 7429338 10-Mar-11 14:11pm
   
You're welcome :)
Dalek Dave 10-Mar-11 19:36pm
   
Good Answer 5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
}

   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

if working then i will explaination
   
Comments
Olivier Levrey 10-Mar-11 4:35am
   
You could have explained right now. Anyway, my 5.
santosh dhanawade 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
}
   
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...
}
santosh dhanawade 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.
Michael Melkonian 10-Mar-11 19:15pm
   
Ok, may be you can understand piece of code that I have just posted. It would help to single step through it and look at values as I don't want to argue about basic stuff all day.
Harrison H 10-Mar-11 19:23pm
   
Ok.

I see double *angle;
I see you call setAngle with a double pointer. I don't see you mention that the OP needs to new the pointer the double pointer points to. Perhaps you meant to imply that, but just taking your parameter change and dunking it into the OPs code is a no go unless he news it there, which is just bad practice anyway. Either way, since he's asking such a basic question, its obvious you should have pointed it out.

No argument there.
Dalek Dave 10-Mar-11 19:37pm
   
Good to see you spotted your own error. 5 for honesty

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 10 Mar 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100