Like Tree1Likes
  • 1 Post By CommonTater

Advance function pointer problem.

This is a discussion on Advance function pointer problem. within the C Programming forums, part of the General Programming Boards category; First please view the below code... Code: #include <stdio.h> #include <string.h> #include <stdlib.h> struct Date { int year, month, day; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Advance function pointer problem.

    First please view the below code...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
        struct Date 
        {
            int year, month, day;
        };
    
        struct Student
        {
            char ID[30];
            char name[30];
            struct Date date_of_birth;
    
        } StudentRec;
    
        void GetStudentRec(StudentRec *st);
        void GetDOB(Date *dt);
    
        int main(void)
        {
    
    
    
        }
    
        void GetStudentRec(StudentRec *st);
        {
            int wish, i;
    
            printf("How many wish: "):
            scanf("%d", &wish);
    
            for(i=0; i<wish; i++)
            {
                printf("Please input student name: ");
                scanf("%[^\n], &(*st[i]).ID);
    
            }
    
    
            
            
        }
    I have try my best to solve the error but I can't so please help...The error below...

    error C2143: syntax error : missing ')' before '*'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Looks like you forgot to use typedef. Without typedef, you should refer to struct Date as struct Date, not as Date. Likewise, StudentRec is an object of type struct Student, not an alias for struct Student.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have a colon instead of a semi-colon at the end of line 32.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Please view below code...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
        struct Date {
    
            int year, month, day;
        };
    
        typedef struct Student {
    
            char ID[30];
            char name[30];
            struct Date date_of_birth[30];
    
        } StudentRec;
    
        
        void GetStudentRec(StudentRec *st);
    
    
        int main(void)
        {
    
            StudentRec stud;
    
            GetStudentRec(&stud);
    
            
    
    
    
    
        }
    
        void GetStudentRec(StudentRec *st)
        {
            int wish, i;
    
            printf("How many wish: ");
            scanf("%d", &wish);
    
            for(i=0; i<wish; i++)
            {
                fflush(stdin);
    
                printf("\nPlease input student ID: ");
                scanf("%[^\n]", &st->ID[i]);
    
                fflush(stdin);
    
                printf("\nPlease input student name: ");
                scanf("%[^\n]", &st->name[i]);
    
                fflush(stdin);
    
                printf("\nPlease input student day: ");
                scanf("%d", &st->date_of_birth[i].day);
    
                fflush(stdin);
    
                printf("\nPlease input student month: ");
                scanf("%d", &st->date_of_birth[i].month);
    
                fflush(stdin);
    
                printf("\nPlease input student year: ");
                scanf("%d", &st->date_of_birth[i].year);
    
                fflush(stdin);
    
            }
    
            for(i=0; i<wish; i++)
            {
                printf("\nID: %s", st.ID[i]);
                printf("\nName: %s", st.name[i]);
                printf("\nDay: %d", st.date_of_birth[i].day);
                printf("\nMonth: %d", st.date_of_birth[i].month);
                printf("\nYear: %d", st.date_of_birth[i].year);
            }
            
            
        }
    How can I print all the student info after the user input? I try to print it but...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) You need st->ID (etc) not st.ID ... This because st is a pointer.

    2) don't use fflush(stdin)... You don't flush your fawcets do?

    3) main is defined correctly as... int main (void).. but at the end you don't return anything: add ... return 0; ... at the closing brace.

    4) you have defined your struct with arrays of 30 ... I hope you don't think this represents 30 students....
    The way to represent 30 students is like this...

    Code:
        struct Date {
    
            int year, month, day;
        };
    
        typedef struct Student {
    
            char ID[10];
            char name[30];
            struct Date date_of_birth;
    
        } StudentRec;
    
        
        void GetStudentRec(StudentRec *st);
    
    
        int main(void)
        {
    
            StudentRec stud[30];
    In which case your array is an array of structs... not a struct of arrays.
    You would access them like this... stud[i].ID ...
    Siaw Ys likes this.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Very thanks...now I able to print all values without problem but I need some help in figure some function prototypes which I don't understand. So please first view the image below...

    Name:  untitled.JPG
Views: 107
Size:  55.5 KB

    The image above show my question...and I don't understand what the purpose of void GetDOB(Date *). This is because I already ask user input all the students info in void GetStudentRec() but now this question require me to create another prototypes for DOB?

    Below is my newest codes...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
        struct Date {
    
            int year, month, day;
    
        };
    
        typedef struct {
    
            char ID[30];
            char name[30];
            struct Date date_of_birth;
    
        } StudentRec[30];
        
        void GetStudentRec(StudentRec *st);
    
    
        int main(void)
        {
            StudentRec stud;
    
            GetStudentRec(&stud);
    
        }
    
        void GetStudentRec(StudentRec *st)
        {
    
            int stud, i;
    
            printf("How many students: ");
            scanf("%d", &stud);
    
            for(i=0; i<stud; i++)
            {
                
    
                printf("\nPlease input student ID: ");
                scanf("%[^\n]", &st[i]->ID);
    
                
    
                printf("\nPlease input student name: ");
                scanf("%[^\n]", &st[i]->name);
    
                
    
                printf("\nPlease input student day: ");
                scanf("%d", &st[i]->date_of_birth.day);
    
                
    
                printf("\nPlease input student month: ");
                scanf("%d", &st[i]->date_of_birth.month);
    
                
    
                printf("\nPlease input student year: ");
                scanf("%d", &st[i]->date_of_birth.year);
    
                
    
            }
    
            for(i=0; i<stud; i++)
            {
                printf("\nID: %s", st[i]->ID);
                printf("\nName: %s", st[i]->name);
                printf("\nDay: %d", st[i]->date_of_birth.day);
                printf("\nMonth: %d", st[i]->date_of_birth.month);
                printf("\nYear: %d", st[i]->date_of_birth.year);
                printf("\n");
            }
            
            
        }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I would do it the way you are... However the assignment seems to want you to call GetDOB from within GetStudentRec... This isn't especially difficult, but it is rather pointless.

    You can just create a separate function above GetStudentRec, but below main to get the dates and call it from GetStudentRec...

    The only thing I would change in your existing code is that line 17 should not specify the array size... that should be done at line 24.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Another new function question helps.

    First please view the below question...

    Write a function called elapsedTime with two parameters, the start time and the end time. Each parameter is a structure with three fields showing the hours, minutes, and seconds of a specific time. The function is to return a time structure containing the time elapsed between the two parameters. You must handle the situation when the start time is in the previous day.

    __________________________________________________ _____________

    My coding so far...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
        struct time
        {
            int hour;
            int min;
            int sec;
    
        } start_time, end_time;
    
        void main ()
        {
            
        }
        
        int elapsedTime(struct start_time*s, struct end_time*e) 
        {
    
            
        }


    My problem is
    I don't know how should I continue...I mean what means by return a time structure containing the time elapsed between the two parameters? And also how I can handle the situation when the start time is in the previous day? I just hope that at least someone can give me some hints to helps me continue...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This should probably be in a new thread... new problem, new thread.
    =====
    Oh my... no functional code at all and already you've got problems...

    DO NOT use void main() ... the correct skeleton for a C program is...
    Code:
    int main (void)
      {
         // your stuff
    
         return 0; 
    }
    Your function declaration for Elapsed time is wrong...
    Code:
    int elapsedTime( struct start_time *s, struct end_time *e)
     { 
    ...

    Best hint I can give you is... THINK about the problem... don't ask us to do your thinking for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with pointer to function problem
    By rob90 in forum C Programming
    Replies: 19
    Last Post: 05-28-2010, 10:33 AM
  2. Pointer to function problem
    By vincent01910 in forum C Programming
    Replies: 6
    Last Post: 08-29-2009, 08:14 AM
  3. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03:50 AM
  4. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 01:41 AM
  5. Replies: 4
    Last Post: 11-05-2006, 01:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21