GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Dec-2004, 09:30
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road

problem on typedef


Dear all,

I am writing a function(actually I am writing several functions for a program) that using typedef,

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int i;

typedef struct{
		int day;
		int month;
		int year;}date;

		date user_input (void);

		void main (){
			user_input();
			
			printf("The date is %d",date.day);
			printf("\nThe month is %d",date.month);
			printf("\nThe year is %d",date.year);
		}
		


		date user_input (void){
			printf ("Enter Date:");
			scanf("%d/%d/%d",&date.day,&date.month,&date.year);
			
		}
			
		

When it complies,it display that :
A:\***_6.c(15) : error C2059: syntax error : 'type'
A:\***_6.c(16) : error C2059: syntax error : 'type'
A:\***_6.c(17) : error C2059: syntax error : 'type'
A:\***_6.c(22) : error C2061: syntax error : identifier 'user_input'
A:\***_6.c(22) : error C2059: syntax error : ';'
A:\***_6.c(22) : error C2059: syntax error : 'type'

I guess I cannot just pass the structure 'date' stuffs into the function for reading the data.Can anyone tell me what's the problem was?and by the way,
I need to declar another structure using typedef,same as 'date',with an addition of name,thus I am going to write in this way:
CPP / C++ / C Code:
typedef struct{
char name[20];
struct date brithday;}

but it is also failure,can any of your experts tell me why?thanks for your help!
  #2  
Old 23-Dec-2004, 10:08
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
The functiom prototypes are fixed:
CPP / C++ / C Code:
date user_input (void);

and the struct date stores the structure including a day,month and a year.
The program ask for user input and display the putput for error checking
  #3  
Old 23-Dec-2004, 10:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
Dear all,

I am writing a function(actually I am writing several functions for a program) that using typedef,

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int i;

typedef struct{
		int day;
		int month;
		int year;}date;

		date user_input (void);

		void main (){
			user_input();
			
			printf("The date is %d",date.day);
			printf("\nThe month is %d",date.month);
			printf("\nThe year is %d",date.year);
		}
		


		date user_input (void){
			printf ("Enter Date:");
			scanf("%d/%d/%d",&date.day,&date.month,&date.year);
			
		}
			
		

When it complies,it display that :
A:\***_6.c(15) : error C2059: syntax error : 'type'
A:\***_6.c(16) : error C2059: syntax error : 'type'
A:\***_6.c(17) : error C2059: syntax error : 'type'
A:\***_6.c(22) : error C2061: syntax error : identifier 'user_input'
A:\***_6.c(22) : error C2059: syntax error : ';'
A:\***_6.c(22) : error C2059: syntax error : 'type'

I guess I cannot just pass the structure 'date' stuffs into the function for reading the data.Can anyone tell me what's the problem was?and by the way,
I need to declar another structure using typedef,same as 'date',with an addition of name,thus I am going to write in this way:
CPP / C++ / C Code:
typedef struct{
char name[20];
struct date brithday;}

but it is also failure,can any of your experts tell me why?thanks for your help!


First of all, you haven't actually created a struct. You have told the C compiler what you mean by "date", but you don't actually have one yet.

CPP / C++ / C Code:
typedef struct {
    int day;
    int month;
    int year;
} date;

int main()
{
  date the_date;

Now you have a struct, named "the_date". How do you get a function to read something into this struct? Well you could pass a pointer to the struct to the function:

CPP / C++ / C Code:
typedef struct {
    int day;
    int month;
    int year;
} date;

int main()
{

    void user_input(date *); /* this is the prototype */

    date the_date;          /* this is the struct you have to work with */

    user_input(&the_date);  /* this calls the function with the address of the struct*/

Now, write the function to handle a pointer to a struct. Something like this:

CPP / C++ / C Code:
void user_input (date *Mydate)
{
 
  scanf("%d.....", &Mydate->day ...);

Regards,

Dave
  #4  
Old 23-Dec-2004, 12:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
The functiom prototypes are fixed:
CPP / C++ / C Code:
date user_input (void);

and the struct date stores the structure including a day,month and a year.
The program ask for user input and display the putput for error checking

I'm sorry, I didn't see your second post at the time I responded to your first one. If you have to do it this way, here's one possibility:

CPP / C++ / C Code:
date user_input ()
{
  date Mydate;

  scanf("%d...", &Mydate.day, ...);

  return Mydate

Then the main looks something like:

CPP / C++ / C Code:
int main (){

  date the_date;
  the_date = user_input();
  
  printf("The date is %d",the_date.day);

  ...


Regards,

Dave
  #5  
Old 23-Dec-2004, 12:11
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Thanks for your help,and one more question
now I have one struct:
CPP / C++ / C Code:
typedef struct {
      int day;
      int month;
      int year}date;

date input;

Now I am writing another struct,using same variable of the struct 'date'
CPP / C++ / C Code:
typedef struct {
     char name[20];
     date brithday;}ndate;

ndate search

Does it works?

search.brithday.day==input.day?
  #6  
Old 23-Dec-2004, 13:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
Thanks for your help,and one more question
now I have one struct:
CPP / C++ / C Code:
typedef struct {
      int day;
      int month;
      int year}date;

date input;

Now I am writing another struct,using same variable of the struct 'date'
CPP / C++ / C Code:
typedef struct {
     char name[20];
     date brithday;}ndate;

ndate search

Does it works?

search.brithday.day==input.day?

No, the == does not automatically hold. You are defining two different structs. Things are equal only if you set them equal.

Here's an example:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int day;
  int month;
  int year;
} date;

typedef struct {
  char name[20];
  date birthday;
} ndate;


date user_input(void);

int main (){

  date the_date;
  ndate the_ndate;

  strcpy(the_ndate.name, "Dave");


  the_date = user_input();

  the_ndate.birthday = the_date;

  printf("The date is %d\n",the_date.day);
  printf("The month is %d\n",the_date.month);
  printf("The year is %d\n",the_date.year);

  printf("\n");

  printf("the_ndate.name = %s\n", the_ndate.name);
  printf("the_ndate.birthday.day = %d\n", the_ndate.birthday.day);
  printf("the_ndate.birthday.month = %d\n", the_ndate.birthday.month);
  printf("the_ndate.birthday.year = %d\n", the_ndate.birthday.year);

  return 0;
}
    
date user_input ()
{
  date Mydate;
  printf ("Enter Date:");
  scanf("%d/%d/%d",&Mydate.day, &Mydate.month, &Mydate.year);
  return Mydate;
}


Regards,

Dave
  #7  
Old 23-Dec-2004, 22:41
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Well,thanks for your guys' help.Now,I wish to open a file form harddisk,if the function returns 0,the other statement goes,otherwise,the program is terminate.

the functoin prototype FILE* open_file (char filename[],char mode) is fixed

Here is my program
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int i;

typedef struct{/*define struct date*/
		int day;
		int month;
		int year;}date;
typedef struct{/*define struct ndate*/
     char name[20];
     date brithday;}ndate;

		
		date user_input (void);/*function prototype*/
		FILE* open_file(char filename[],char mode[]);/*function prototype*/
		date input;/*define date*/
		ndate search;/*define ndate*/
		int main (){
			user_input();
			
			printf("The input.day is %d",input.day);
			printf("\nThe input.month is %d",input.month);
			printf("\nThe input.year is %d",input.year);
			if ((open_file("brithday.txt","r"))==1){
				printf("\nError!");}
			if ((open_file("brithday.txt","r"))==0){
				printf("\nok!");}
		}
		


		date user_input (void){
			printf ("Enter Date:");
			scanf("%d/%d/%d",&input.day,&input.month,&input.year);
			return (input);
		}
			
		FILE* open_file(char filename[],char mode[]){

			if ((fopen(filename,mode))==NULL){
				printf ("\nFile cannot be opened");
				return 1;}
			
			else
				printf ("\nYour file is okay!");
			return 0;}

The program can do what I want to do,it can return 0/1 depends on the file exits or not.But,the complier told me that Ive errors:
C:\Documents and Settings\User\桌面\***_6.c(24) : warning C4047: '==' : 'struct _iobuf *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\***_6.c(42) : warning C4047: 'return' : 'struct _iobuf *' differs in levels of indirection from 'const int '

How can I change my code in order to fit what I want to do(use the function as a control statement)
  #8  
Old 23-Dec-2004, 22:46
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Dave,

Here I have 2 struct
CPP / C++ / C Code:
typedef struct{
int day;
int month;
int year;}date;

typedef struct{
char name[20];
date brithday;}ndate;

void man()
date input;
ndate search;

}

Does search.brithday.* consists the SAME STRUCTURE as the sturct input?(they carries different values)
Example:search.brithday.day...etc
  #9  
Old 24-Dec-2004, 00:33
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by fwongmc
Does search.brithday.* consists the SAME STRUCTURE as the sturct input?(they carries different values)
Example:search.brithday.day...etc
Yes, the search.birthday structure is of type date, and the input structure is of type date. Although, just a note... you misspelled "birthday."

I have a question about your open_file function. The function's return value is a pointer to a FILE, so why does your code return an integer value? Also, you have some problems with your file I/O. Since you never assign the return value of fopen() to a FILE pointer, the value is lost in memory, and the file is not closed. One more thing... please attempt clearer style when coding. Your style makes reading your code difficult. Go ahead and take a look at this tutorial, written by our very own WaltP.
__________________
-Aaron
  #10  
Old 24-Dec-2004, 01:24
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Well,thanks for your guys' help.Now,I wish to open a file form harddisk,if the function returns 0,the other statement goes,otherwise,the program is terminate.

the functoin prototypeFILE* open_file (char filename[],char mode) is fixed

Here is my program
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int i;

typedef struct{/*define struct date*/
		int day;
		int month;
		int year;}date;
typedef struct{/*define struct ndate*/
     char name[20];
     date brithday;}ndate;

		
		date user_input (void);/*function prototype*/
		FILE* open_file(char filename[],char mode[]);/*function prototype*/
		date input;/*define date*/
		ndate search;/*define ndate*/
		int main (){
			user_input();/*execute function user_input*/
			
			printf("The input.day is %d",input.day);/*check only*/
			printf("\nThe input.month is %d",input.month);/*check only*/
			printf("\nThe input.year is %d",input.year);/*check only*/
			if (!open_file("brithday.txt","r")){
				printf("\nOK!");}
			else{
				printf("\nError!");}
		}
		


		date user_input (void){/*function user_input main*/
			printf ("Enter Date:");
			scanf("%d/%d/%d",&input.day,&input.month,&input.year);
			return (input);
		}
			
		FILE* open_file(char filename[],char mode[]){/*function open_file main*/

			if ((fopen(filename,mode))==NULL){
				printf ("\nFile cannot be opened");
				return 1;}
			
			else
				printf ("\nYour file is okay!");
			return 0;}







The program can do what I want to do,it can return 0/1 depends on the file exits or not.But,the complier told me that Ive errors:
C:\Documents and Settings\User\桌面\***_6.c(42) : warning C4047: 'return' : 'struct _iobuf *' differs in levels of indirection from 'const int '


How can I change my code in order to fit what I want to do(use the function as a control statement)
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
still problem with polynom linked list if13121 C Programming Language 7 23-Nov-2004 00:02
linked list problem if13121 C Programming Language 10 11-Nov-2004 13:34
c simple question problem with switch case if13121 C Programming Language 2 25-Oct-2004 02:06
typedef struct problem grizli C Programming Language 8 19-Jun-2004 17:32
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.