Help with copying an array into a struct

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 2
Reputation: Sluga is an unknown quantity at this point 
Solved Threads: 0
Sluga Sluga is offline Offline
Newbie Poster

Help with copying an array into a struct

 
0
  #1
Apr 9th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,839
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Help with copying an array into a struct

 
0
  #2
Apr 9th, 2005
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];
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 2
Reputation: Sluga is an unknown quantity at this point 
Solved Threads: 0
Sluga Sluga is offline Offline
Newbie Poster

Re: Help with copying an array into a struct

 
0
  #3
Apr 10th, 2005
Ok thanks heaps I thought it might be the case.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC