Reputation: 2394
I am passing an example of a spheroidal harmonic equation from C to C++ from the following
document.
In doing this I am facing a problem of a declaration I have never seen float ***c
. The example can be seen in the same source at page 777. Specifically the function is the one below:
void solvde(…, float **y, float ***c, float **s);
I saw some old posts explaining some not usual declaration. Also this post was somehow useful but that did not give me a full understanding of what I am looking for. I have been researching a lot but didn't find any good explanation. Additionally on the same source provided there is no explanation for that.
Can someone explain or give a small simple example of this kind of declaration and how to use it, and, most importantly, what would be its equivalent in C++?. Thanks for pointing in the right direction.
Upvotes: 2
Views: 3159
Reputation:
void solvde(…, float **y, float ***c, float **s);
as
type *a ;
means a
is a pointer to type, substitued a
with *a
means a is a pointer to a pointer to type and so on,
declaration equivalent, as an array with its preset dimension size, is
float y[][5] = { {4,5,7.0}, {0.9, 7}, {5.8} };
float c[9][5][7] ={};
float s[2][7] ={};
1st would be array of 3 x 5 dim. size of floating type number, and 9 x 5 x 7 and 2 x 7 dim. size for the others
the difference here it is constant pointer, so 1st is float** const y
with total 3 x 5 x float size is allocated statically ie. in compile time
Upvotes: 0
Reputation: 238401
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.
Upvotes: 4
Reputation: 15586
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> > >
Upvotes: 0