Thread: simple get() function to fill instance in struct Please tell me why it wont work =D!

  1. #1
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11

    simple get() function to fill instance in struct Please tell me why it wont work =D!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
        struct employee
        {
            char firstName[20];
            char lastName[20];
            float rate;
        };
    
    
    struct employee emp;
    
    
    void get(struct emp, int* pi);
    
    
    int main()
    {
        char input[30];
        char *pinput = NULL;
        int i = 0;
        int *pi = &i;
    
    
    
    
        get(struct emp, pi);  //<--- error here (Expected expression before 'struct' I don't know what that means
    
    
        printf("first name %s \n", emp.firstName);
        printf("last name %s \n", emp.lastName);
    
    
        return 0;
    }
    //*********************
    void *get(struct emp, int *pi)
    {
        pinput = fgets(input,30,stdin);
        strncpy((emp+*pi)->firstName,30,stdin);
    }
    I don't know why it's not working =(, please tell me =).

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    struct employee
        {
            char firstName[20];
            char lastName[20];
            float rate;
        };
    
    struct employee emp;
    Here you define a struct employee and then declare a variable "emp" of this type, i.e. struct employee.

    Code:
    void get(struct emp, int* pi);
    Here you declare the first parameter to be of type struct emp, but there is no such struct in your program.

    Code:
    get(struct emp, pi);  //<--- error here (Expected expression before 'struct' I don't know what that means
    Suppose you have a function which is declared as
    Code:
    void some_function(int n);
    How would you call it (assume "x" is an int)?

    Code:
    some_function(int x);
    // or 
    some_function(x);
    Do you understand what your problem is?

    Bye, Andreas

  3. #3
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11
    I feel stupid now lol.... I thought I understood but I was wrong... tried dereferencing and a few other things still not working out for me o.0....

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    It looks like you don't understand what a struct is.

    Your struct employee is a type just like the built-in types int, double, char, ... Thus you have to use it accordingly.

    I recommend rereading your learning resources about structs or read for example Structures in C - Tutorial - Cprogramming.com or struct (C programming language) - Wikipedia, the free encyclopedia

    Bye, Andreas

  5. #5
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11
    Quote Originally Posted by AndiPersti View Post
    It looks like you don't understand what a struct is.

    Your struct employee is a type just like the built-in types int, double, char, ... Thus you have to use it accordingly.

    I recommend rereading your learning resources about structs or read for example Structures in C - Tutorial - Cprogramming.com or struct (C programming language) - Wikipedia, the free encyclopedia

    Bye, Andreas
    Took your advice and here is what I came up with. Unfortunately, it still doesn't work how I would like it to... o.0 But I believe it's a drastic improvement from the previous post =) thank you for taking time to help me I greatly appreciate it.

    Here is the new code
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include <time.h>
    
         struct employee    {
            char firstName[20];
            char lastName[20];
            float rate;
         };
     struct employee emp[20];
    
     void get(struct employee, int*);
    
     int main()
     {
    for(int c = 0; c< 20;c++)
    {
    
         get(emp, c);
    
         printf("first name %s \n", emp.firstName);
         printf("last name %s \n", emp.lastName);
    }
    
         return 0;
     }
    
     //*********************
     void get(struct employee emp, int *pi)
     {
         char *pinput;
         char input[20];
         pinput = fgets(input,30,stdin);
         strncpy(emp.firstName,input,20);
    }

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    "emp" has been defined globaly as an array of employee structs

    I can't see where you treat emp as a struct array in your code...

    It might be a good idea if you declare "emp" as a single employee struct and get one instance working first

    Also, there is no point in getting 30 from stdin with the fgets if you are only using 20
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-16-2012, 11:47 AM
  2. Novice Beginner: Simple hello world wont work
    By hern in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2005, 12:16 PM
  3. My Pop function wont work - can you help?
    By lindilou in forum C++ Programming
    Replies: 0
    Last Post: 05-04-2003, 02:31 PM
  4. Replies: 2
    Last Post: 05-04-2003, 05:30 AM
  5. CustomControlClass wont show first instance
    By btq in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2002, 03:10 PM