float and 2D array?

Posted on 2003-02-28
Medium Priority
Last Modified: 2012-08-13
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
Question by:torgrimb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8041743
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;

Accepted Solution

iRBIs earned 300 total points
ID: 8042021
This code is OK. Maybe it is someth with compiler. (What compiler do you use?)


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

Expert Comment

ID: 8043165
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8044514
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.

Author Comment

ID: 8044519
You gave me some new ideas on how to solve this issue

Expert Comment

ID: 8044615
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.


Author Comment

ID: 8044698
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question