Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing struct to function

Tags:

c

function

struct

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:

like image 311
Daniel Del Core Avatar asked Apr 29 '12 05:04

Daniel Del Core


People also ask

Can you pass a struct to a function?

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.

How do you send a struct to a function in C++?

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.

Can we pass structure to function by value in C?

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.


2 Answers

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; } 
like image 72
Donnell Avatar answered Sep 29 '22 10:09

Donnell


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.

like image 29
MByD Avatar answered Sep 29 '22 11:09

MByD