14,102,245 members
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

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

## 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
}```

## Solution 1

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

if working then i will explaination
Olivier Levrey 10-Mar-11 4:35am

You could have explained right now. Anyway, my 5.

function uotside the class doesn't take right parameter mostly took a garbage value becouse this have memory assignment or some thing else.

## 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
}
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...
}

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

Top Experts
Last 24hrsThis month
 OriginalGriff 286 RickZeeland 100 Richard MacCutchan 75 Kschuler 75 CHill60 70
 OriginalGriff 3,821 Richard MacCutchan 1,635 Patrice T 1,137 Gerry Schmitz 910 Maciej Los 908