Problem with variable type extern const

I have this on a main program provided by the professor and the spec states it should not be modified.


#include <cstdlib>
#include <iostream>
using namespace std;

extern const int DIM0, DIM1, DIM2, DIM3;
extern float ***ptr4D[];

int main()
{
int value;

// Store all values.
value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
for (int ix1 = 0; ix1 < DIM1; ++ix1)
for (int ix2 = 0; ix2 < DIM2; ++ix2)
for (int ix3 = 0; ix3 < DIM3; ++ix3)
ptr4D[ix0][ix1][ix2][ix3] = float(value++);

// Test all stored values.
value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
for (int ix1 = 0; ix1 < DIM1; ++ix1)
for (int ix2 = 0; ix2 < DIM2; ++ix2)
for (int ix3 = 0; ix3 < DIM3; ++ix3)
if (ptr4D[ix0][ix1][ix2][ix3] != value++)
{
cout << "Error at: ptr4D[" << ix0 << "][" << ix1 << "][";
cout << ix2 << "][" << ix3 << "]\n";
return EXIT_FAILURE;
}

cout << "Success!\n";
return EXIT_SUCCESS;
}


I have this header file that I created and has a content of:

#define myHdrh_H
#ifdef myHdrh_H
extern const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5;
float p[DIM0][DIM1][DIM2][DIM3] = {0};
extern float ***ptr4D[] = &(***p[0]);//&p[0][0][0][0];
#endif


And then I have another .cpp file which only include the header file.
and has this.

#include "myHdr.h"

my problem is the pointer variable ***ptr4D[]
when I do a rebuilt I get this error " cannot convert from type float* to type float ***[]

from this
extern float ***ptr4D[] = &(***p[0]);//&p[0][0][0][0];

float ***ptr4D[] is type "float ***[]" while &(***p[0] or &p[0][0][0][0] is type "float*", so obviously they are not compatible.

can anybody tell me how to get a type ***[] from this "float p[DIM0][DIM1][DIM2][DIM3] = {0}"

sincerely


First:

1
2
extern const int DIM0, DIM1, DIM2, DIM3;
extern float ***ptr4D[];


This indicates that these variables are defined in another translation unit (another .cpp file.) Normally such extern declarations would be in a header file and not thrown willy-nilly into a .cpp file.

In C++ there would be no need for these const ints to be declared extern. But, this is what your instructor gave you to work with, so I guess you deal with the cuckoo professor. Seeing as they are declared extern, the proper place to define them would probably be in the same translation unit as ptr4D.

extern float ***ptr4D[] is an array of pointers to pointers to pointers to float.

float p[DIM0][DIM1][DIM2][DIM3] = {0}; is essentially a 1-D array of floats that the compiler allows you to treat as if it was multi-dimensional. Not surprisingly, the definition (which shouldn't be in the header, by the way) doesn't match the declaration - an array of type float is not an array of type pointer to pointer to pointer to float.

I would expect ptr4D to be defined in some translation unit (.cpp file) other than the one main is in as: float *** ptr4D[DIM0] ; and presumably each element would be initialized to point to a dynamically allocated array of pointers (whose elements would be initialized to point to dynamically allocated arrays of pointers...) by some function which resides in the same translation unit. (Although, since you apparently can't modify main, that assumption would appear to be wrong.)

Useful info:
http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c
Last edited on
it's a lan.......
cire,

On the main.cpp whic is the start of the topic there are two extern declarations one for the dimensions and one for the array of pointer to pointer to pointer of type float. This is the professors program which we are not to modify.

The header file is something I did. float p[DIM0][DIM1][DIM2][DIM3] = {0}; is a variable definition so I can set aside a space in memory so all I need to do is equate the pointer declaration float ***ptr4d[] to the pointer p. This is when I have the problem.

You may say the professor is cuckoo but he gives us this exercise for a reason, he will almost tie your hand but there is always a solution, he just does not want it to be a piece of cake.
float p[DIM0][DIM1][DIM2][DIM3] = {0}; is a variable definition so I can set aside a space in memory so all I need to do is equate the pointer declaration float ***ptr4d[] to the pointer p. This is when I have the problem.


Variable definitions don't belong in header files, and this one isn't up to the problem. You need space for pointers-to-pointers-to-float, pointers-to-float, and the floats they point to, and you need to tie them all together.


he just does not want it to be a piece of cake


Cuckoo.
Its the solution I gave you.
http://www.cplusplus.com/forum/general/81640/

You need space for pointers-to-pointers-to-float, pointers-to-float, and the floats they point to, and you need to tie them all together.

@cire is correct, in both his statements: The one above and cuckoo. It is tedious to create all of the arrays but it will work. I did it, the program compiled and it printed out the "Success!".
1
2
3
4
5
6
7
// Now define p00, p01,p02 and p10,p11,p12
// p00, p01, etc... should have dimension DIM2
// and keep going until you get to an array of floats
// i.e. p000[], p001[], etc... would be just floats
float **p0[] = {p00,p01,p02};
float **p1[] = {p10,p11,p12};
float ***ptr4D[] = {p0,p1};
I will only agree with cire's first statement, because I tried it.
it took 33 definitions/declarations to get ***ptr4D[]. Then I send it to the professor and he said, That is what I want you to learn what not to do. Next meeting I will show you how to do it correctly.

The professor works for Space and Naval Warfare Systems Command (SPAWAR) a high tech command of the US Navy and designing embedded system and programming top security projects. His style of teaching may be weird but it works.

I would compare his assignment to a request to drive from San Diego to Los Angeles. He would require his students to show him how to drive from San Diego to Los Angeles, but he would tie your hand. He will not let you use Interstate 5, not use Insterstate 15, not use Interstate 10, 405, not to use State highway 60, 91, 78, 52 and just use the side road and you only have 10 gallons of gas.

Isn't that a more challenging assignment, he ties your hand, leaves you with almost no option and finds out if you could do it. With this problem you learn how to think, and check different approach and pick the best.

With another professor who ask for solutions without any restriction their students will always say use Interstate 5 and the solution is over. That is no fun at all.
Topic archived. No new replies allowed.