943,898 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 950
  • C RSS
Sep 30th, 2007
0

changing values of a struct

Expand Post »
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. }
Similar Threads
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 30th, 2007
0

Re: changing values of a struct

  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();
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 30th, 2007
0

Re: changing values of a struct

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.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 30th, 2007
0

Re: changing values of a struct

>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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 30th, 2007
0

Re: changing values of a struct

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,
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 30th, 2007
0

Re: changing values of a struct

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 30th, 2007
0

Re: changing values of a struct

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!
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: wht will be the o/p?
Next Thread in C Forum Timeline: newline character





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


Follow us on Twitter


© 2011 DaniWeb® LLC