11 Replies - 2402 Views - Last Post: 11 December 2012 - 01:18 PM Rate Topic: -----

#1 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

2D Arrays: cannot convert from 'int *' to 'int'

Posted 10 December 2012 - 02:51 PM

I've been working on a program which outputs a PPM pixelmap file after drawing it into a 2D array. I believe I have the idea down and most of it working but I'm running into the following error:

error C2440: '=' : cannot convert from 'int *' to 'int'


The code:
void drawRectangle( int x, int y, int width, int height, int *pixArr[], int rgb[] )
{
	char temp[16];
	int i = 0, j = 0;

	pixArr[i] = new int [resolution[1]];
	for ( i = 0; i < resolution[1]; ++i )
	{
		pixArr[i][j] = new int [resolution[0]];
		for ( j = 0; j < resolution[0]; ++j )
		{
			pixArr[i][j] = new int [3];
			if ( ( j >= x && j <= ( j + resolution[0] ) ) && ( i <= y && i >= ( i - resolution[1] ) ) )
			{
				pixArr[i][j][0] = rgb[0];
				pixArr[i][j][1] = rgb[1];
				pixArr[i][j][2] = rgb[2];
			}
			else
			{
				pixArr[i][j][0] = 0;
				pixArr[i][j][1] = 0;
				pixArr[i][j][2] = 0;
			}
		}
	}
}


The error occurs on Line 9.

This post has been edited by DevonZ: 10 December 2012 - 03:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 2D Arrays: cannot convert from 'int *' to 'int'

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 10 December 2012 - 03:57 PM

		pixArr[i][j] = new int [resolution[0]];

pixArr[i ][j ] is an integer, however you are attempting to assign it the value of a pointer (array specifically). This isn't allowed since an int and int* are two different types. If you want to create the array on the spot (instead of having it filled before being passed in to you - I would suggest checking into this so you don't just have random memory in use that is never actually used), you should do something along these lines:
		pixArr[i] = new int [resolution[0]];

Since pixArr[i ] is an int* you are allowed to assign it the value of an int*.

I didn't look through the program to ensure that there aren't other errors, but this should help you (or the compiler) find them if there are.

Hopefully that makes sense.
Was This Post Helpful? 0
  • +
  • -

#3 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 10 December 2012 - 04:02 PM

If you look on line 6, I already have that. I'm trying to make it a 2 or even 3-D array...
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 10 December 2012 - 04:26 PM

I suggest you study the following tutorial: Intro to dynamic memory allocation. You are currently not allocating memory properly for a two dim. array. This tutorial should help explain how to properly dynamically allocate a multidimensional array.

I do have a question though. Is there any reason you aren't using std::vector instead?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 10 December 2012 - 10:11 PM

Thanks for the link, I'll take a look at it.

I'm not using vectors because I've not learned about them yet and the purpose of this project/exercise is to gain a better understanding on the implementation of multidimensional arrays. :)
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 06:57 AM

Then if this is just about getting a better understanding of multidimensional arrays do you really need to be complicating this issue with dynamic memory. Maybe you should start by using statically allocated arrays before you jump into the dynamic arrays.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 07:07 AM

Your life would be made dramatically easier with a simple struct:
struct Color {
	int r, g, b;
};



Your code for pixArr is confusing. A class for your image wouldn't be a bad idea:
class Image {
public:
	const int width, height;
	Image(int width, int height);
	~Image();
	void set(int, int, const Color &);
	Color get(int, int) const;
private:
	Color *data;
};



I'm not real sure why you're calling new all over the place when really all you want to do is draw...
Was This Post Helpful? 0
  • +
  • -

#8 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 10:09 AM

The assignment is very specific on drawing the image into an array and then looping the array and outputting it to a file. It specifies that I am to be using dynamic allocation and multidimensional arrays.

Coming from a background in PHP; I know it's not a very practical approach, but it's part of the assignment so that's' what I have to go by. Mainly, just grasping a strong understanding of pointers is going to be my biggest struggle since PHP basically hides all of that by managing it for you...

As for my code, I believe I am trying to make it into a 3D array[i][j][0] but have only defined it as a 2D **array.

This post has been edited by DevonZ: 11 December 2012 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#9 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 11:05 AM

@jimblumberg I finally got it working, thanks to the link you posted on multidimensional arrays.

void drawRectangle( int x, int y, int width, int height, int **pixArr[], int rgb[] )
{
	char temp[16];
	int i = 0, j = 0;

	pixArr = new int **[resolution[1]];
	for ( i = 0; i < resolution[1]; ++i )
	{
		pixArr[i] = new int *[resolution[0]];
		for ( j = 0; j < resolution[0]; ++j )
		{
			pixArr[i][j] = new int [3];
			if ( ( j >= x && j <= ( j + resolution[0] ) ) && ( i <= y && i >= ( i - resolution[1] ) ) )
			{
				pixArr[i][j][0] = rgb[0];
				pixArr[i][j][1] = rgb[1];
				pixArr[i][j][2] = rgb[2];
			}
			else
			{
				pixArr[i][j][0] = 0;
				pixArr[i][j][1] = 0;
				pixArr[i][j][2] = 0;
			}
		}
	}
}


However, when I try to access the array from outside of that function, it's unable to read the memory. Aren't arrays passed by reference? Shouldn't changes I make to the variable in that function be accessible by the calling function upon returning?

To clarify, I define pixArr in main() and pass pixArr to drawRectangle(). Once draw rectangle completes its tasks and we return to main(), should pixArr not be populated?

I tested the contents of pixArr within the drawRectangle() and it worked fine. When attempting the same loop on pixArr in main(), it's unable to read the memory.

This post has been edited by DevonZ: 11 December 2012 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 11:38 AM

View PostDevonZ, on 11 December 2012 - 12:09 PM, said:

The assignment is very specific on drawing the image into an array and then looping the array and outputting it to a file. It specifies that I am to be using dynamic allocation and multidimensional arrays.


Fair enough.


View PostDevonZ, on 11 December 2012 - 12:09 PM, said:

I believe I am trying to make it into a 3D array[i][j][0] but have only defined it as a 2D **array.


Correct. This is why a struct would help.


You should NOT be trying to create that array in a draw function. Also, your global var resolution should not exist. It's unfortunate.

You should really have another function to create your dynamic array.

pixArr = new int **[resolution[1]];



You have just clobbered the variable passed as pixArr. Nothing is getting passed back. You are now working from a local copy.

Again, I would strongly suggest a struct or two.

If you must have giant multi array mess, consider something like:
int ***createSilly3DArray(int width, int height);

void deleteSilly3DArray(int ***imgData, int width, int height);

void drawRectangle(int ***imgData, int imgWidth, int imgHeight, int x, int y, int width, int height, int rgb[] ) {
	for (int px=0; px<width && (x+px)<imgWidth; px++ ) {
		/* your code here */
	}
}


Was This Post Helpful? 1
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 12:02 PM

Quote

Shouldn't changes I make to the variable in that function be accessible by the calling function upon returning?

When you pass an array, you actually create a local pointer. Pointing this local pointer to newly allocated memory only affects that local pointer. Once the function ends, that local pointer is cleared off of the stack and your memory that you allocated is now lost.

Here is a simple example to get the point across
#include <iostream>

using namespace std;

//Sets the local pointer to null
void setToNull(int *ptr) {
   ptr = NULL;
}

int main() {
   int *ptr = new int;
   std::cout<<"ptr points to "<<ptr<<std::endl;
   setToNull(ptr);
   std::cout<<"ptr points to "<<ptr<<std::endl;

   return 0;
}


The above takes in an integer pointer and sets that pointer to NULL. If you run this program, you will notice that after the function call, ptr back in main is left unchanged. This is because a copy of ptr was made when it is passed to the function. Any changes made to where that pointer points only affects that local pointer. To actually made changes to where a pointer points and have it reflect back in main, you can either pass a double pointer, or a reference of a pointer.

i.e.
#include <iostream>

using namespace std;

//Sets the ptr from main to null
void setToNull(int *&ptr) {
   ptr = NULL;
}

int main() {
   int *ptr = new int;
   std::cout<<"ptr points to "<<ptr<<std::endl;
   setToNull(ptr);
   std::cout<<"ptr points to "<<ptr<<std::endl;

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: 2D Arrays: cannot convert from 'int *' to 'int'

Posted 11 December 2012 - 01:18 PM

Thanks guys, I ended up passing it by as mentioned by jjl. Everything is working good now. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1