How to convert double * to double?

Any ideas for this typeing problem?

Here is what I am trying to do. This is not real code:

LinkedList* angles;
double dblangle; 
dblangle = (some function for angle calculation returning double value);
(double*)LinkedListCurrent(angles) = &double; 

I hope you get this idea. The problem is with the last line. Initially, angles are of type void*, so I must first convert it to double*.

+3
source share
6 answers

You use a unary operator *to dereference a pointer. Highlighting a pointer means retrieving the specified value to get the value of the original type.

// Create a double value
double foo = 1.0;
// Create a pointer to that value
double *bar = &foo;
// Extract the value from that pointer
double baz = *bar;

edit 2 : (removed edit 1, as it is not relevant to your actual question, but was based on a misunderstanding)

, , , , , void * double *. * , , , .

// Get a void * pointing to our double value.
void *vp = &foo;
// Now, set foo by manipulating vp. We need to cast it to a double *, and
// then dereference it using the * operator.
*(double *)vp = 2.0;
// And get the value. Again, we need to cast it, and dereference it.
printf("%F\n", *(double *)vp);

, , LinkedListCurrent void *, , . :

*(double*)LinkedListCurrent(angles) = dbangle;

, , LinkedListCurrent, dbangle. , , .

, LinkedListCurrent, . . , , .

, , , , , , , . , - , , , , . .

edit 3. , , , ; . , . , , , , dynamic_cast, ( dynamic_cast , ).

+8

?

:

double d = 1.0;  // create a double variable with value 1.0
double *dp = &d; // store its address in a pointer
double e = *dp;  // copy the value pointed at to another variable
+2

:

(double*)LinkedListCurrent(angles) = &double;

&double, &dbangle. , :

((double*)LinkedListCurrent(angles)) = &dbangle;

, .

+1

. ( ), , .

union double_and_ptr {
    double d;
    double *p;
};

double_and_ptr x, y;
x.d = 0.1;
y.p = &x.d; // store a pointer in the same place as a double
y.d = x.d * 1.2; // store a double in the same place as a ptr
0

reinterpret_cast.

double foo = 3.0;
double* pfoo = &foo;
double bar = reinterpret_cast<double>(pfoo);
0

:

, . . ?

- :

// declare a pointer to a double
double *pointer_to_double; 

// declare a double
double my_double = 0.0;

// use the indirection operator (*) to dereference the pointer and get the value
// that it pointing to. 
my_double = *pointer_to_double;

:

void print_chance_of_snow(double *chance)
{
    double d = *chance;
    d = d * 100; // convert to a percentage
    printf("Chance of snow is: %.2f%%\n", d);
}

int main(int argc, char *argv[])
{
    double chance_of_snow = 0.45;
    print_chance_of_snow(&chance_of_snow); 
}
0

Source: https://habr.com/ru/post/1726514/


All Articles