float and 2D array?

What's wrong with this:

float **figureMatrix;
figureMatrix = new float*[ 20 ];

I get errors like:

- int differs in levels of indirection from **float
- cannot convert from **float to int
- missing storage-class or type specifiers

Do I have to use 'int' to specify the number of pointers to pointers? I know this works with int, but I cannot figure out the errors I get on this one.

// Torgrim
Who is Participating?
iRBIsConnect With a Mentor Commented:
This code is OK. Maybe it is someth with compiler. (What compiler do you use?)


float ** figureMatrix;
figureMatrix = new (float*)[20];
you started out fine but you need another step: now you need to allocate those 20 pointers to create the second dimension (say the size of that dimension is 100.

for (int i = 0; i < 20; i++)
  figureMatrix[i] = new float[100];

also be sure to delete when you are done using the matrix.

for (i = 0; i < 20; i++)
  delete [] figureMatrix[i];

delete [] figureMatrix;
jtm111 shows one possible way.

An alternative way would be:
float **figureMatrix;
figureMatrix = new float[ 20 ][ 100 ];

What's the difference?
- jtm111 demonstrated how to allocate an array of pointers and to allocate for each pointer an array of float.
- My alternative uses as single allocation of the whole twodimensional float object.

Allocation of a single block is of course easier.
One advantage of jtm111's solution is that each
array may have a different length.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

torgrimbAuthor Commented:
Thanks for all the comments! I did try out your suggestions, sorry to say it did not solve my problem.

jtm111:  I'm aware that I need to initialize the array with values, but the errors are from these two lines, and I'm not able to get to where I fill in the values.

iRBSs:   I'm using VC7. I do not think it is compiler specific, but hey, who knows.

stefang: I'll have to initialize the second arrays one by one, so I'll have to go with the jtm111 way.

It might just be that the code is actually fine, but that there is some machine-specific thing.
torgrimbAuthor Commented:
You gave me some new ideas on how to solve this issue
Check the code immediately above and after your declaration of float **figureMatrix... the errors you are getting tend to be associated with typographical mistakes, not programming logic. Are you terminating all lines with semi-colon;

Also are you sure somewhere you are not trying to assign figureMatrix to an integer left hand side?

intVariable = figureMatrix;

That's what the compiler error seems to suggest.

Or perhaps you are passing figureMatrix to a function that expects an integer argument when you think it takes a pointer.

// you think it is this
void math_func(float **m);

// but it is really this
void math_func(int intArgument);

// so you do this

I would be very surprised if the problem were machine related. VC7 should be good enough for something like this.

Good luck dude.

torgrimbAuthor Commented:
jtm111: I've tried the things you suggested, and yes: the (ehhrr 'damn') semicolon is there, so it's not that.

It's just the two lines that gives those error messages, which is strange, because I've got no 'int' in there. I even tried to change the argument to 20.0, just to be sure that this was not the int.

It might just be that I have a bug on my machine that I'm working on right now. I'll be setting up the problem on another machine on monday, but for now: have a nice Weekend.
All Courses

From novice to tech pro — start learning today.