I'm a new C programmer and I wanted to know how I can pass a struct
through to a function. I'm getting an error and can't figure out the correct syntax to do it. Here is the code for it....
Struct:
struct student{ char firstname[30]; char surname[30]; }; struct student person;
Call:
addStudent(person);
Prototype:
void addStudent(struct student);
and the actual function:
void addStudent(person) { return; }
Compiler errors:
line 21: warning: dubious tag declaration: struct student
line 223: argument #1 is incompatible with prototype:
Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing the entire structure to the function.
Passing structure to function in C++ In this program, user is asked to enter the name , age and salary of a Person inside main() function. Then, the structure variable p is to passed to a function using. displayData(p); The return type of displayData() is void and a single argument of type structure Person is passed.
A struct may be more complex than an array, but it's perfectly possible to pass it as value, and it may be inefficient, but the reason that you can't pass an array by value is because it is defined as a pointer by default.
This is how to pass the struct
by reference. This means that your function can access the struct
outside of the function and modify its values. You do this by passing a pointer to the structure to the function.
#include <stdio.h> /* card structure definition */ struct card { int face; // define pointer face }; // end structure card typedef struct card Card ; /* prototype */ void passByReference(Card *c) ; int main(void) { Card c ; c.face = 1 ; Card *cptr = &c ; // pointer to Card c printf("The value of c before function passing = %d\n", c.face); printf("The value of cptr before function = %d\n",cptr->face); passByReference(cptr); printf("The value of c after function passing = %d\n", c.face); return 0 ; // successfully ran program } void passByReference(Card *c) { c->face = 4; }
This is how you pass the struct
by value so that your function receives a copy of the struct
and cannot access the exterior structure to modify it. By exterior I mean outside the function.
#include <stdio.h> /* global card structure definition */ struct card { int face ; // define pointer face };// end structure card typedef struct card Card ; /* function prototypes */ void passByValue(Card c); int main(void) { Card c ; c.face = 1; printf("c.face before passByValue() = %d\n", c.face); passByValue(c); printf("c.face after passByValue() = %d\n",c.face); printf("As you can see the value of c did not change\n"); printf("\nand the Card c inside the function has been destroyed" "\n(no longer in memory)"); } void passByValue(Card c) { c.face = 5; }
The line function implementation should be:
void addStudent(struct student person) { }
person
is not a type but a variable, you cannot use it as the type of a function parameter.
Also, make sure your struct is defined before the prototype of the function addStudent
as the prototype uses it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With