Hi, i'm doing a program in OpenGL and need some help with arrays.
Basically I have a structure called ObjectNode which is used to implement a linked list with two data members, an int for the object type and an array of IntPoints (IntPoint is just a simple structure to represent a cartesian point).

Now in one of my functions I create an IntPoint array with size MAX (2 in this case). It gives the points in the array some values and then calls the function to add them to the linked list (AddObject(int type, IntPoint[])).

Now I got my program to output the points on the first line of the AddObject function and it did it sucessfully, so I know the point array was passed in properly. My objective now is to create a new ObjectNode and assign the new objects IntPoint array to the one passed into the function.
However when I try to do this I get the error
error C2440: '=' : cannot convert from 'struct IntPoint []' to 'struct IntPoint [2]'
Can someone help me with this? I can't seem to assign the array within the struct to the one passed in. I think this is because since arrays are passed by value i'm trying to assign a pointer of an array to an array, but am not sure. If so do I have to change the declaration of ObjectNode so it contains a pointer to an array rather than an actual array? Or do I have to copy each value separatly into the ObjectNode object's array? I'll post the relevant code below

struct IntPoint
{
   int x, y;
};

const int MAX = 2;

// structure for node containing object data
struct ObjectNode
{ 
	int type;
	IntPoint PtList[MAX];
	ObjectNode *next;
};

void mouse(int button, int state, int x, int yc)
{
   IntPoint pt = {x, 480 - yc};
   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
      if (PointInRect(pt, quit_icon))
         exit(0);
      else if (PointInRect(pt, rectangle_icon))
              current_type = RECTANGLE;
      else if (PointInRect(pt, drawing_region) && current_type != 0)
      {       
              IntPoint PtList[MAX];
			  switch(current_type)
			  {
			  case RECTANGLE : PtList[0] = pt;
				               PtList[1].x = pt.x + 80;
							   PtList[1].y = pt.y + 60;
                               break;
              }
			  AddObject(current_type, PtList);
			  DrawObject(current_type, PtList);
			  glFlush();
      }

}

void AddObject(int type, IntPoint PtList[])
{
	ObjectNode * newObj;
	newObj = new ObjectNode; // Create a new object
	newObj->type = type;
	newObj->PtList = PtList; /// ERROR - THIS IS WHAT I CAN'T DO///
	newObj->next = NULL; // Since its the last object there is no 'next' object
	Last->next = newObj; // Assign 'next' object of the previous last model to this new object
	Last = newObj;  // Set 'last' to point to the object we just added

}

Code tags added. -Narue

An array is not a modifiable lvalue, so you can't assign to one using the = operator. You need to copy each element from the source array to the destination array individually. This is best done with a loop, but since your array only has two elements, you can do it inline:

newObj->PtList[0] = PtList[0];
newObj->PtList[1] = PtList[1];
This article has been dead for over six months. Start a new discussion instead.