473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

error C2440

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;

}
May 7 '06 #1
1 1073
Allen Maki wrote:
// 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
May 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Qiao Yun | last post by:
I used vc++.net (visual studio .net ) to open a project which can work well in vc++6.0. I succeeded in compiling the project in vc++.net in release mode . But when I tried to compile the project...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
0
by: Allen Maki | last post by:
/* I am trying to practice multidimensional array in a managed class using dynamical allocation. If I tried to compile the following codes, I would get the under mentioned c2440 error...
2
by: teddybyte | last post by:
my script below is: #include "stdafx.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, ...
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.