Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

error C2440

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Allen Maki - 07 May 2006 21:59 GMT
Hi everybody,

Thanks for your suppoty.

//Why I got the following error message? for the 4th line:

// error C2440: '=' : cannot convert from

//'long (*)[3]' to 'long *' Types pointed

//to are unrelated; conversion requires

// reinterpret_cast, C-style cast or

//function-style cast

#include <iostream>

using namespace std;

int main()

{

const int max = 4; //(1)

const int max2 = 3; //(2)

long *pprime; //(3)

pprime = new long[max][max2]; //(4)

return 0;

}
Tamas Demjen - 08 May 2006 18:16 GMT
> // error C2440: '=' : cannot convert from
>
> //'long (*)[3]' to 'long *' Types pointed

That's because the left hand side doesn't match the type of the right
hand side.

The best way to use 2D arrays is to flatten them. Allocate a 1D array of
the size columns * rows:

long * pprime = new long[columns * rows];

and calculate the linearized index on your own, like this:

pprime[row * columns + column] = 0;

You need to take care of deleting your array when it's not used anymore:

delete [] pprime;

If you absolutely need the [row][column] syntax, or if the number of
columns is different in each row, then you have to allocate an array of
pointers, and then for each row allocate an array that holds the columns:

long** matrix = new long[rows];
for(int i = 0; i < rows; ++i)
   matrix[i] = new long[columns];

matrix[row][column] = whatever;

[...]

for(int i = 0; i < rows; ++i)
   delete [] matrix[i];
delete [] matrix;

I strongly recommend that you use std::vector if you program in C++, and
use a 1D vector instead of a 2D one.

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.