You can do it like this:
printf("%.6f", myFloat);
6 represents the number of digits after the decimal separator.
Answer from Roman Byshko on Stack OverflowVideos
float is a type name of floating point type. Floatin point numbers are used to represent a subset of rational numbers with finite precision.
Given a type name T, T* is a type name that is a pointer type. Specifically, it is a pointer to the type T. A pointer is used to indirectly "refer" (point) to another object. The value of a pointer is the memory address where the pointed object is stored. It is possible to get the value of the pointed object by indirecting through the pointer.
Pointers are often used as an iterator to traverse through elements of an array. This is made possible by "pointer arithmetic": By adding 1 to a pointer, changes it to point to the successor element of an array.
Thus, float* is a pointer to float. Furthermore float** is a pointer to a float* i.e. a pointer to a pointer to a float. Finally, float*** is a pointer to a float** i.e. a pointer to a pointer to a pointer to a float.
what would be its equivalent in C++?
Pointers exist in C++ as well as pointers to pointers and pointers to pointers to pointers.
I saw some old posts explaining some not usual declaration.
It is quite rare to need a "three-star" pointer in C, and extremely rare to need it in C++, but there is nothing special about it.
The other answers answer your question well, and are correct in that this is also valid C++. However, I would be inclined to say that a "more C++11" equivalent of the triple-star would be:
std::vector<std::vector<std::vector<float>>>
Before C++11, this would be a little uglier:
std::vector<std::vector<std::vector<float> > >
The difference is
You are dereferencing the
intand casting it tofloatinprintf("(float)* : %f\n", (float)*iptr);which is fine.
You are casting the
intpointer to afloatpointer, and printing thefloatpointer with the"%f"specifier is undefined behavior, the correct specifier for printing pointers is"%p", soprintf("(float*) : %f\n", (float*)iptr);is wrong, it should be
printf("(float*) : %p\n", (void *) iptr);casting to
float *here is not meaningful, because thevoid *address is the same as thefloat *address and also theint *address, the difference would be when you do pointer arithmetic.You are casting the
intpointer to afloatand dereferencing the resultingfloatpointer, although it will violate strict aliasing rules inprintf("(float*) : %f\n", *(float*)iptr);which is also undefined behavior
The first one is correct.
i is an int variable, and iptr a pointer to that int.
(float)*iptr:*iptrdereferencesiptr, which returns anint. Then thatintis converted to a temporaryfloatcontaining the same value. And thatfloatis used byprintf.*(float*)iptr: Attempts to cast a pointer-to-intinto a pointer-to-float. This is invalid, and should produce a compiler warning or error. It creates a pointer with the same address, but with the type saying that it points to afloatvalue.The
*operator then dereferences it, so theintis read as if it were afloat. So the resultingfloatwould be invalid, and it could result in a segfault becausefloats are longer thanints, so it reads more memory than there is allocated for theint.(float*)iptr: Same problem, but it doesn't dereference the (invalid) pointer, and passes a pointer-to-floatintoprintf, instead of afloat. Butprintfexpects afloat. Some compilers should also produce a warning/error here because the format string indicates what value types are expected.If the format specifier indicates
%p, it expects a pointer (void*,float*, or any other). It will then print out the address, and not the value it points to. This can be useful in debugging for example.