| | |
changing values of a struct
Thread Solved
![]() |
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
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,
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,
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> // for drand48 #include <math.h> struct atom{ double x,y,z; double vx,vy,vz; double fx,fy,fz; int id; }; void initialpos(struct atom *name, int N); const int N=100; int main() { struct atom *patom[100]; int i; for (i = 0; i < N; i++) { patom[i]=(struct atom *)malloc(sizeof(struct atom)); } for (i = 0; i < N; i++) { if (patom[i]==NULL) { printf("out of MEM\n"); return 1; } } /* give atoms their initial position */ initialpos(patom[0],N); for (i = 0; i < N; i++) { printf("pos. of %d: %f\t %f\t %f\t \n", i, patom[i]->x, patom[i]->y, patom[i]->z); } return 0; } void initialpos(struct atom *name, int N) { int i; for (i = 0; i < N; i++) { name[i].x=drand48()*20; name[i].y=drand48()*20; name[i].z=drand48()*20; printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z); } }
•
•
•
•
c Syntax (Toggle Plain Text)
void initialpos(struct atom *name, int N) { int i; for (i = 0; i < N; i++) { name[i].x=drand48()*20; name[i].y=drand48()*20; name[i].z=drand48()*20; printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z); } }
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();
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
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)
Maybe I'm silly, but I just don't see it.
(and thanks for helping)
C Syntax (Toggle Plain Text)
void initialpos(struct atom *name, int N) { int i; for (i = 0; i < N; i++) { name[i]->x=drand48()*20; name[i]->y=drand48()*20; name[i]->z=drand48()*20; printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z); } }
Last edited by theraven1982; Sep 30th, 2007 at 8:24 pm.
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
Now it's like this:
(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,
C Syntax (Toggle Plain Text)
void initialpos(struct atom *name, int N) { int i; for (i = 0; i < N; i++) { name[i]->x=drand48()*20; name[i]->y=drand48()*20; name[i]->z=drand48()*20; printf("pos of %d: %f\t %f\t %f\t \n", i, name[i]->x, name[i]->y, name[i]->z); } }
(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,
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.
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.
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Photoshop - javascript slice : error : copy command not available?? (JavaScript / DHTML / AJAX)
- help with Java (Java)
- Problems with changing values returned by RegQueryValueEx (C)
- Browser keeps getting reset to <none>..etc (Viruses, Spyware and other Nasties)
- tweaking my amd 64 (Motherboards, CPUs and RAM)
- The improtance of replacing values in pointers (C)
- Any chance of a little help? (Java)
Other Threads in the C Forum
- Previous Thread: wht will be the o/p?
- Next Thread: newline character
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






