944,066 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4732
  • C RSS
Apr 9th, 2005
0

Help with copying an array into a struct

Expand Post »
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
  1. struct IntPoint
  2. {
  3. int x, y;
  4. };
  5.  
  6. const int MAX = 2;
  7.  
  8. // structure for node containing object data
  9. struct ObjectNode
  10. {
  11. int type;
  12. IntPoint PtList[MAX];
  13. ObjectNode *next;
  14. };
  15.  
  16. void mouse(int button, int state, int x, int yc)
  17. {
  18. IntPoint pt = {x, 480 - yc};
  19. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  20. if (PointInRect(pt, quit_icon))
  21. exit(0);
  22. else if (PointInRect(pt, rectangle_icon))
  23. current_type = RECTANGLE;
  24. else if (PointInRect(pt, drawing_region) && current_type != 0)
  25. {
  26. IntPoint PtList[MAX];
  27. switch(current_type)
  28. {
  29. case RECTANGLE : PtList[0] = pt;
  30. PtList[1].x = pt.x + 80;
  31. PtList[1].y = pt.y + 60;
  32. break;
  33. }
  34. AddObject(current_type, PtList);
  35. DrawObject(current_type, PtList);
  36. glFlush();
  37. }
  38.  
  39. }
  40.  
  41. void AddObject(int type, IntPoint PtList[])
  42. {
  43. ObjectNode * newObj;
  44. newObj = new ObjectNode; // Create a new object
  45. newObj->type = type;
  46. newObj->PtList = PtList; /// ERROR - THIS IS WHAT I CAN'T DO///
  47. newObj->next = NULL; // Since its the last object there is no 'next' object
  48. Last->next = newObj; // Assign 'next' object of the previous last model to this new object
  49. Last = newObj; // Set 'last' to point to the object we just added
  50.  
  51. }
Code tags added. -Narue
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sluga is offline Offline
2 posts
since Apr 2005
Apr 9th, 2005
0

Re: Help with copying an array into a struct

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:
  1. newObj->PtList[0] = PtList[0];
  2. newObj->PtList[1] = PtList[1];
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 10th, 2005
0

Re: Help with copying an array into a struct

Ok thanks heaps I thought it might be the case.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sluga is offline Offline
2 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Using sprites...
Next Thread in C Forum Timeline: I want itz solution!! can anyone tell me the soltuion!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC