Tutorial :C Prog.: Structure as argument of function?


I am trying to to create structure "Date of birth", and function that will assign values to the structure, and i am wondering is that possible to do that somehow like this:

(PS. I am constantly getting error "Argument list syntax error", for 2nd and 23th lines.)

#include <stdio.h>  void input (dob_st *);  int main ()  {      typedef struct      {          int year;          int month;          int day;      }      dob_st;        dob_st date;      dob_st *p;      p=&date;      input (*p);      printf("%02i.",p->day);      printf("%02i.",p->month);      printf("%i.",p->year);        return 0;  }  void upis (dob_st *p)  {      printf ("Date of birth:\nDay?\n");      scanf ("%i",&(p->day));      printf ("Month?\n");      scanf ("%i",&(p->month));      printf ("Year?\n");      scanf ("%i",&(p->year));  }  


I think you're getting this because the definition of dob_st is local only to main -- it's not defined outside this method. The void input (dob_st *); method has no way to know what the dob_st is, and it's called upis later on, not input, which is probably another error.


Some things: You should declare your structure outside of the main function, and before the prototype of the input function.

You should also pass to input a pointer to a dob_st structure and not a structure itself: The possible code now as

dob_st date;  dob_st *p;  p=&date;  input (*p);  

should be

dob_st date;  input (&date);  

and the output be later based on date directly.

PD: Is the upis function be meant to be the input function?


You're changing the name of the function from 'input' to 'upis'.

You also need to define dob_st outside the main() function.


p is a pointer, so the argument should be "input(p)" in stead of "inuput(*p)". Using *p deferenences the pointer, and it is not necessary in this case.


In standard C, you write "struct dob_st *p" instead of simply "dob_st *p". Also make sure that you declare the struct dob_st before you first use it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »