changing values of a struct

Thread Solved
Reply

Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

changing values of a struct

 
0
  #1
Sep 30th, 2007
Hello,

i'm still trying to learn C, and I have a new question .

I want to declare a struct, and use a function to manipulate the entries. If I change the values within a function it's ok; however, when i access the members outside the function, they're not defined anymore.

How do i change the values in the struct such that a different function can use it as well? I thought using a pointer to struct would be 'good enough', but apparently it isn't .

Thanks in advance,

  1. #include <stdio.h>
  2. #include <stdlib.h> // for drand48
  3. #include <math.h>
  4.  
  5. struct atom{
  6. double x,y,z;
  7. double vx,vy,vz;
  8. double fx,fy,fz;
  9. int id;
  10. };
  11.  
  12. void initialpos(struct atom *name, int N);
  13.  
  14. const int N=100;
  15.  
  16. int main()
  17. {
  18. struct atom *patom[100];
  19.  
  20. int i;
  21. for (i = 0; i < N; i++)
  22. {
  23. patom[i]=(struct atom *)malloc(sizeof(struct atom));
  24. }
  25.  
  26. for (i = 0; i < N; i++)
  27. {
  28. if (patom[i]==NULL)
  29. {
  30. printf("out of MEM\n");
  31. return 1;
  32. }
  33. }
  34.  
  35. /* give atoms their initial position */
  36. initialpos(patom[0],N);
  37.  
  38. for (i = 0; i < N; i++)
  39. {
  40. printf("pos. of %d: %f\t %f\t %f\t \n", i, patom[i]->x, patom[i]->y, patom[i]->z);
  41. }
  42.  
  43.  
  44. return 0;
  45. }
  46.  
  47. void initialpos(struct atom *name, int N)
  48. {
  49. int i;
  50.  
  51. for (i = 0; i < N; i++)
  52. {
  53. name[i].x=drand48()*20;
  54. name[i].y=drand48()*20;
  55. name[i].z=drand48()*20;
  56. printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
  57. }
  58. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,009
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: changing values of a struct

 
0
  #2
Sep 30th, 2007
Originally Posted by theraven1982 View Post
  1. void initialpos(struct atom *name, int N)
  2. {
  3. int i;
  4.  
  5. for (i = 0; i < N; i++)
  6. {
  7. name[i].x=drand48()*20;
  8. name[i].y=drand48()*20;
  9. name[i].z=drand48()*20;
  10. printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
  11. }
  12. }
Taking this as an example:
name[i].x=drand48()*20;
You are assigning to a field of a structure, but that is not a field of a structure. It is a pointer to a field of a structure. It can be done in two ways:
(*name[i]).x = drand48() * 20; or
name[i]->x = drand48() * 20;
Same thing is going on with the others and even with the parameters in the printf();
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: changing values of a struct

 
0
  #3
Sep 30th, 2007
I tried changing the function like that as well; but it still doesn't work.
Maybe I'm silly, but I just don't see it.

(and thanks for helping)

  1. void initialpos(struct atom *name, int N)
  2. {
  3. int i;
  4.  
  5. for (i = 0; i < N; i++)
  6. {
  7. name[i]->x=drand48()*20;
  8. name[i]->y=drand48()*20;
  9. name[i]->z=drand48()*20;
  10. printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
  11. }
  12. }
Last edited by theraven1982; Sep 30th, 2007 at 8:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,009
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: changing values of a struct

 
0
  #4
Sep 30th, 2007
>but it still doesn't work.
What's not working?. What's supposed to happen and it isn't?.
You forgot to change the arguments also in the printf().
Last edited by Aia; Sep 30th, 2007 at 8:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: changing values of a struct

 
0
  #5
Sep 30th, 2007
Now it's like this:

  1. void initialpos(struct atom *name, int N)
  2. {
  3. int i;
  4.  
  5. for (i = 0; i < N; i++)
  6. {
  7. name[i]->x=drand48()*20;
  8. name[i]->y=drand48()*20;
  9. name[i]->z=drand48()*20;
  10. printf("pos of %d: %f\t %f\t %f\t \n", i, name[i]->x, name[i]->y, name[i]->z);
  11. }
  12. }

(i forgot the printf function the previous time).
Now it's complaining about 'invalid type argument of '->' '
I'll explain what i want to do.
In main() a the function initialpos() is called, to give a particle some initial position. But this position is needed later on as well, so i want to keep the values too when the function is finished.
When i did it like in the opening post, the values are written to the struct, but after the function is fnished, the elements are gone.

Hope you understand what I mean. Thanks for the help so far,
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,009
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: changing values of a struct

 
0
  #6
Sep 30th, 2007
Needs some changes:
initialpos(&patom[0],N); instead of initialpos(patom[0],N); the & is needed if you want to pass it the address of a structure.
void initialpos(struct atom *name[], int N) instead of void initialpos(struct atom *name, int N) *name[] is the beginning of the array of structures.
Last edited by Aia; Sep 30th, 2007 at 9:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: changing values of a struct

 
0
  #7
Sep 30th, 2007
Thanks! it's working now. It makes sense: before I only gave one element as an argument, but instead I had to give an array with starting address &patom[0]. Now all variables in the struct are kept (because the array in the argument).
I'd never think of that! (even though it makes sense now). Thanks a lot! Now I can finally go to bed.. it's 2 AM here .
(If something doesn't work, I keep wondering how to solve it... and then I can't sleep. )
Thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC