By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Passing and processsing multiple pointers to a function inside C

P: 5
Hello,all!
In my program I use some arrays. In one of the functions I pass 2 of the lists call by reference,i.e. I pass it the pointers. Inside the function I change the values of the arrays. Inside the function everything works fine, but when I quit it and return to the main function, the values do not preserve.
Why is this problem, can it be fixed somehow?
Thanks,
mshr25
Jun 8 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Perhaps a code sample?
Jun 8 '07 #2

P: 5
I'll try to post the code now:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. unsigned int NodeNum,TriNum;
  7.  
  8. struct Point_C
  9. {
  10.     double x;
  11.     double y;
  12.     double z;
  13. };
  14. typedef struct Point_C PointCart;
  15.  
  16.  
  17. struct Tri
  18. {
  19.     PointCart v1;
  20.     PointCart v2;
  21.     PointCart v3;
  22. };
  23. typedef struct Tri TriCart;
  24.  
  25. /*-------------------------------------------------------------------------------------------*/
  26.  
  27. /* defining a structure for a list of triangles' centres */
  28.  
  29. typedef struct TriList 
  30.   PointCart data;              /* center of a triangle */
  31.   struct TriList *next;        /* pointer to a next element in a list */ 
  32. } TriList;
  33.  
  34. /*-------------------------------------------------------------------------------------------*/
  35.  
  36. /* defining a structure for a single cube */
  37.  
  38. typedef struct CubeData 
  39.   PointCart Origin;            /* the origin of the cube */
  40.   double EdgeLength;           /* the edgelength of the cube */
  41.   unsigned int NumOfChildren;  /* number of the children cubes */
  42.   unsigned int NumOfTria;      /* number of mesh triangles the cube contains */
  43.   TriList *List;               /* linked list to all the triangles that belong to the cube */
  44. } CubeData;
  45.  
  46. /* defining subroutines for processing lists*/
  47.  
  48. TriList* InsertTri(TriList *L_head, PointCart data)
  49. /* inserts a new element "data" into the first place of a linked list defined by "L_head" */
  50.  
  51. {
  52.     TriList *l_new;
  53.     l_new = (TriList*)malloc(sizeof (TriList));
  54.     l_new->data = data;
  55.     l_new->next = L_head;
  56.     return l_new;
  57. }
  58.  
  59. TriList *DeleteTri(TriList *L_head)
  60. /* deletes the first element of a linked list defined by "L_head" */
  61.  
  62. {
  63.     TriList* curr = L_head;
  64.     if (curr==NULL)
  65.         return curr;
  66.  
  67.     curr = curr->next;
  68.     free(L_head);
  69.     return curr;
  70.  
  71. }
  72.  
  73. TriList *FreeListTri(TriList *L_head)
  74. /* frees the linked list defined by "L_head" recursively */
  75.  
  76. {
  77.     TriList *tail;
  78.     if (L_head == NULL)
  79.         return NULL;
  80.     tail = L_head->next;
  81.     free(L_head);
  82.     return FreeListTri(tail);
  83. }
  84.  
  85. /* subroutines for working with the type CubeData*/
  86.  
  87. CubeData CreateChildCube(CubeData *ParentCube,unsigned int index)
  88. /* given a pointer to the ParentCube the subroutine creates a child cube with a given index */
  89.  
  90. {
  91.     CubeData ChildCube;
  92.     double Length=ParentCube->EdgeLength;
  93.     double x0=ParentCube->Origin.x;
  94.     double y0=ParentCube->Origin.y;
  95.     double z0=ParentCube->Origin.z;
  96.  
  97.  
  98.     ChildCube.EdgeLength=Length/2;
  99.     ChildCube.NumOfChildren=0;
  100.     ChildCube.NumOfTria=0;
  101.     ChildCube.List=NULL;
  102.  
  103.  
  104.     switch(index)
  105.     {
  106.     case 0:
  107.         ChildCube.Origin.x=x0; ChildCube.Origin.y=y0; ChildCube.Origin.z=z0; break;
  108.     case 1:
  109.         ChildCube.Origin.x=x0+Length/2; ChildCube.Origin.y=y0; ChildCube.Origin.z=z0; break;
  110.     case 2:
  111.         ChildCube.Origin.x=x0+Length/2; ChildCube.Origin.y=y0+Length/2; ChildCube.Origin.z=z0; break;
  112.     case 3:
  113.         ChildCube.Origin.x=x0; ChildCube.Origin.y=y0+Length/2; ChildCube.Origin.z=z0; break;
  114.     case 4:
  115.         ChildCube.Origin.x=x0; ChildCube.Origin.y=y0; ChildCube.Origin.z=z0+Length/2; break;
  116.     case 5:
  117.         ChildCube.Origin.x=x0+Length/2; ChildCube.Origin.y=y0; ChildCube.Origin.z=z0+Length/2; break;
  118.     case 6:
  119.         ChildCube.Origin.x=x0+Length/2; ChildCube.Origin.y=y0+Length/2; ChildCube.Origin.z=z0+Length/2; break;
  120.     case 7:
  121.         ChildCube.Origin.x=x0; ChildCube.Origin.y=y0+Length/2; ChildCube.Origin.z=z0+Length/2; break;
  122.     }
  123.     return ChildCube;
  124. }
  125. void PopulateCubes(TriList *List,CubeData *Cubes,PointCart Origin,unsigned int Lengtht)
  126. {
  127.     TriList *Pos=List;
  128.     PointCart data;
  129.     double Length=Cubes[0].EdgeLength*2;
  130.     double Xpos;
  131.     double Ypos;
  132.     double Zpos;
  133.     int count=0;
  134.  
  135.     while (List!=NULL)
  136.     {
  137.         count++;
  138.         data=List->data;
  139.         Xpos=(data.x-Origin.x)/Length>=0.5;
  140.         Ypos=(data.y-Origin.y)/Length>=0.5;
  141.         Zpos=(data.z-Origin.z)/Length>=0.5;
  142.  
  143.         if ((Xpos==0) & (Ypos==0) & (Zpos==0))
  144.         {
  145.             Cubes[0].List=InsertTri(Cubes[0].List,data);
  146.             Cubes[0].NumOfTria=Cubes[0].NumOfTria+1;
  147.             List=DeleteTri(List);
  148.             continue;                    
  149.         }
  150.  
  151.         if ((Xpos==1) & (Ypos==0) & (Zpos==0))
  152.         {
  153.             Cubes[1].List=InsertTri(Cubes[1].List,data);
  154.             Cubes[1].NumOfTria=Cubes[1].NumOfTria+1;
  155.             List=DeleteTri(List);
  156.             continue;                    
  157.         }
  158.  
  159.         if ((Xpos==1) & (Ypos==1) & (Zpos==0))
  160.         {
  161.             Cubes[2].List=InsertTri(Cubes[2].List,data);
  162.             Cubes[2].NumOfTria=Cubes[2].NumOfTria+1;
  163.             List=DeleteTri(List);
  164.             continue;    
  165.         }
  166.  
  167.         if ((Xpos==0) & (Ypos==1) & (Zpos==0))
  168.         {
  169.             Cubes[3].List=InsertTri(Cubes[3].List,data);
  170.             Cubes[3].NumOfTria=Cubes[3].NumOfTria+1;
  171.             List=DeleteTri(List);
  172.             continue;
  173.         }
  174.  
  175.         if ((Xpos==0) & (Ypos==0) & (Zpos==1))
  176.         {
  177.             Cubes[4].List=InsertTri(Cubes[4].List,data);
  178.             Cubes[4].NumOfTria=Cubes[4].NumOfTria+1;
  179.             List=DeleteTri(List);
  180.             continue;            
  181.         }
  182.  
  183.         if ((Xpos==1) & (Ypos==0) & (Zpos==1))
  184.         {
  185.             Cubes[5].List=InsertTri(Cubes[5].List,data);
  186.             Cubes[5].NumOfTria=Cubes[5].NumOfTria+1;
  187.             List=DeleteTri(List);
  188.             continue;    
  189.         }
  190.  
  191.         if ((Xpos==1) & (Ypos==1) & (Zpos==1))
  192.         {
  193.             Cubes[6].List=InsertTri(Cubes[6].List,data);
  194.             Cubes[6].NumOfTria=Cubes[6].NumOfTria+1;
  195.             List=DeleteTri(List);
  196.             continue;        
  197.         }
  198.  
  199.         if ((Xpos==0) & (Ypos==1) & (Zpos==1))
  200.         {
  201.             Cubes[7].List=InsertTri(Cubes[7].List,data);
  202.             Cubes[7].NumOfTria=Cubes[7].NumOfTria+1;
  203.             List=DeleteTri(List);
  204.             continue;                    
  205.         }
  206.  
  207.     }
  208. }
  209. int main()
  210. {
  211.     CubeData ZeroCube;
  212.     CubeData Children[8];
  213.     int i;
  214.     TriCart *Tri;
  215.     unsigned int TriSize;
  216.     TriList *List;
  217.  
  218.                 ZeroCube.Origin.x=-0.15;
  219.     ZeroCube.Origin.y=-0.15;
  220.     ZeroCube.Origin.z=-0.15;
  221.     ZeroCube.EdgeLength=0.3;
  222.     ZeroCube.NumOfChildren=0;
  223.     ZeroCube.NumOfTria=0;
  224.     ZeroCube.List=NULL;
  225.  
  226.  
  227.  
  228.     for (i=0;i<=7;i++)
  229.     {
  230.         Children[i]=CreateChildCube(&ZeroCube,i);
  231.     }
  232.  
  233.     Tri=FileRead(&TriSize);
  234.     List=BuildTriList(Tri,TriSize);
  235.     PopulateCubes(List,Children,ZeroCube.Origin,TriSize);
  236.  
  237.  
  238.  
  239.     free(Tri); 
  240.     return 0;
  241. }
  242.  
I have a triangular surface mesh that represents a surface.
I have a structure of a point:PointCart and a structure for a triangle TriCart. I read the data from the file and create an array of triangles. After that I create a list of triangle centers-basically it's a linked list of the type TriList. Then I create a cube that circumscribes the surface and divide it into 8 child cubes. I pass the array of these child cubes to the PopulateCubes together with the list of the triangles' centers. The problem is that after this subroutine the list should be empty pointing to NULL, but it contains everytime to elements.
Thanks
mshr25
Jun 9 '07 #3

P: 5
The duplicate post was deleted
Jun 9 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:

void PopulateCubes(TriList *List,CubeData *Cubes,PointCart Origin,unsigned int Lengtht)

etc...
shows List as a pointer to a TriList.

The variable List is a copy of the pointer used on the call to PopulateCubes(). Therefore, any change to List just changes the copy. The pointer in main() is unchanged,

You need the address of the pointer in the calling function in oreder to change it. I think you need to try:

Expand|Select|Wrap|Line Numbers
  1. void PopulateCubes(TriList **List,CubeData *Cubes,PointCart Origin,unsigned int Lengtht)
  2.  
  3. etc...
  4.  
and you make the call in main by:

Expand|Select|Wrap|Line Numbers
  1. PopulateCubes(&List,Children,ZeroCube.Origin,TriSiz  e);
  2.  
You will need to fix PopulateCubes() so that every use of List is changed to *List.
Jun 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.