C Question: How do you pass structures (or structure fields) as function parameters?
use pass by pointer.... struct mystruct { ... }; void passthestruct(struct mystruct* p) { // use the structure pointer } pass by value: void passstructbyvalue(struct mystruct v) { } just like passing variable to function
I declared my structures using typedef: typedef struct { char OrigCurr[4]; char WantCurr[4]; float ExRate; } STRUCTTYPE_Rates; typedef struct { char User[31]; char Pass[31]; char Role[14]; } STRUCTTYPE_Users; STRUCTTYPE_Rates STRUCT_Rates[100]; STRUCTTYPE_Users STRUCT_Users[100]; I'm trying to pass the parameter by pointer: int TXTDATA_Read_Rates(STRUCTTYPE_Rates* STRUCT_Rates, FILE *STREAM_Rates) { //blahblah } int TXTDATA_Read_Users(STRUCTTYPE_Users* STRUCT_Users, FILE *STREAM_Users) { //blahblah } Now when I compile, i get the following error message, for both lines above: error: expected ‘)’ before ‘*’ token What am I doing wrong?
I'm using gcc btw. I'm required to.
Also, my struct declarations are in the main function. Is there a way to do what I want without making the structures global?
change the structure declarations as... typedef struct _mystruct { fields... } mystruct; here the error maybe due to structure name is not declared. Alternate: struct _mystruct { fields }; typedef struct _mystruct mystruct;
I've got these errors from trying to compile the attached .c file: test.c:74: error: expected ‘)’ before ‘a’ test.c:99: error: expected ‘)’ before ‘STRUCT_Users’ test.c:153: error: expected ‘)’ before ‘STRUCT_Users’ test.c:175: error: expected ‘)’ before ‘STRUCT_Users’ test.c:210: error: expected ‘)’ before ‘STRUCT_Users’
Join our real-time social learning platform and learn together with your friends!