Thread: C Programming: Structures

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    6

    C Programming: Structures

    Hey everyone,

    I am trying to complete an assignment for school and I've run into a pretty large roadblock. I have multiple errors when attempting to compile it. Here is the assignment:

    First, we need to create a Header file.
    1. The file will contain three constants NAME_SIZE 80, SSN_SIZE 13, NUM_PEOPLE 10.
    2. The file needs a typedef structure named Person which has a field for name(NAME_SIZE), social security number with dashes (SSN_SIZE), and an integer for year of birth.
    3. The file will need prototypes (function declaration minus the body) for the functions to be created in the .c file.


    Second, we need to create our .c file which includes the functions, with bodies, listed below:
    1. getOnePerson: This takes a reference to a Person as a parameter. This function should get a single name, ssn, and year of birth from the terminal using scanf and put that data into the Person Structure.
    2. printOnePerson: This takes a Person (by value, not by reference) as a parameter. This function should print the name, ssn, and year from the structure in the following format: John_Heimershmidt:111-11-1111:1862 There are no spaces before and after the colons.
    3. getPeople: This takes an array of Person structures and number of people to get. Call getOnePerson for each Person in the array.
    4. printPeople: This takes an array of Person structures and a number of people to print. This method should print that many Person object from the array.


    Third, a test program has been provided to test out our code. It may not be altered.



    Here is the header file I created. It is called assign3.h:


    Code:
    #ifndef assign3_h
    #define assign3_h
    
    /*
    #define NAME_SIZE 80;
    #define SSN_SIZE 12;
    #define NUM_PEOPLE 10;
    */
    
    typedef struct
    {
            char name[80];
            char ssn[12];
            int yob;
    }Person;
    
    void getOnePerson(Person *person);
    void printOnePerson(Person person);
    void getPeople(Person *person[], int people);
    void printPeople(Person person[], int people);
    
    #endif
    *****Notice I have commented out the global variable definitions. I was receiving errors when I attempted to define them in my header file; however I included them in my .c file and it worked. This is not a priority but if someone knows how I can fix this that would be great.



    Here is my assign3.c file:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "assign3.h"
    
    #define NAME_SIZE 80;
    #define SSN_SIZE 12;
    #define NUM_PEOPLE 10;
    
    
    void getOnePerson(Person *person)
    {
            Person p;
            char *name;
            char ........n;
            int *yob;
    
            scanf("%s", name);
            strncpy(p.name, name, NAME_SIZE);
    
            scanf("%s", ssn);
            strncpy((p.ssn), ssn, SSN_SIZE);
    
            scanf("%d", yob);
            p.yob = &yob;
    
            *person = &p;
    }
    
    void printOnePerson(Person p)
    {
            printf("%s:%s:%d\n", p.name, p.ssn, p.yob);
    }
    
    void getPeople(Person *person[], int num)
    {
            int i;
            for(i=0; i<num; i++)
            {
                   getOnePerson(*person[i]);
            }
    }
    
    void printPeople(Person person[], int num)
    {
        int i;
            for(i=0; i<num; i++)
        {
                   printOnePerson(*person[i]);
            }
    }


    Here is the assign3Test.c file. This file may not be altered but I figured I'd include it anyway.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "assign3.h"
    
    int main()
    {
            Person people[NUM_PEOPLE];
    
            printf("Getting people\n");
            getPeople(people, 3);
    
            printf("\nPrinting people\n");
            printPeople(people, 3);
    }


    Here are the errors I receive when I attempt to compile the program. Also, I am running short I am time so any suggestions to lead me in the right direction are greatly appreciated.

    Code:
    assign3Test.c: In function âmainâ:
    assign3Test.c:7: error: âNUM_PEOPLEâ undeclared (first use in this function)
    assign3Test.c:7: error: (Each undeclared identifier is reported only once
    assign3Test.c:7: error: for each function it appears in.)
    assign3Test.c:7: warning: unused variable âpeopleâ
    assign3.c: In function âgetOnePersonâ:
    assign3.c:21: error: expected â)â before â;â token
    assign3.c:24: error: expected â)â before â;â token
    assign3.c:27: warning: assignment makes integer from pointer without a cast
    assign3.c:29: error: incompatible types when assigning to type âPersonâ from type âstruct Person *â
    assign3.c: In function âgetPeopleâ:
    assign3.c:42: error: incompatible type for argument 1 of âgetOnePersonâ
    assign3.c:13: note: expected âstruct Person *â but argument is of type âPersonâ
    assign3.c: In function âprintPeopleâ:
    assign3.c:51: error: invalid type argument of âunary *â (have âPersonâ)

    I know this is a lot of errors. This is only my third c program and my first one involving structures. I also seem to become confused when attempting to pass by value versus passing by reference. I have spent hours on this already and have gotten to the point where anything new I am trying just produces more errors.

    Please let me know if you have any suggestions. Thank you all for your time!

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Correction: Line 14 of assign3.c should say:

    char ........n;

    My apologies, not sure how that happened.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Evidently something strange happens with the formatting when i attempt to write line 14 of assgin3.c.

    It is a declaration for a character. It is named ssn. There is an asterisk before ssn.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The '#define'-lines are for the pre-processor and should not contain a semicolon.

    If the pre-processor read this:
    Code:
    …
    #define NAME_SIZE 80;
    …
            strncpy(p.name, name, NAME_SIZE);
    …
    it will produce this code:
    Code:
    …
            strncpy(p.name, name, 80;);
    …
    Delete the semicolon at end of the define-lines and they will work in the header-file.
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Thank you for your response Woodstokk. I changed that in my header file and I never have a new error that comes up in my header file which says:

    assign3.h:17: note: expected âstruct Person **â but argument is of type âstruct Person *â

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    In your main-function the function 'getPeople' will called as:
    Code:
    Person people[NUM_PEOPLE];
    …
    getPeople(people, 3);

    'people' is an array with 10 members of type Person.
    If you use an array without an index, then the address of the first array member is the result.
    So, if you write 'people', it is the same as if you write '&people[0]' (address of first member from the array).


    Your function 'GetPeople' will be called with a pointer to type Person (behind this pointer there are more then one member but this dosn't matter),
    but the definition says it takes an array of pointers of type Person.
    'Person *person[]' is like 'Person **person', but you became 'Person *person'.
    Sorry, i'm not a nativ english speaker. I hope everyone else can explain it better.
    The point is that the function should define like:
    Code:
    void getPeople(Person *person, int num)
    {
        int i;
        for(i = 0 ; i < num ; i++)
        {
            getOnePerson(&person[i]);
        }
    }

    The next problem i see is the function 'getOnePerson'.
    You define pointer to characters, but you have no place for the characters it self.
    The scanf-calls will result in seg-faults, because the pointers was never initialized and point to an address that not yours.
    Second, you don't need a local type Person. you can copy directly to the structure that you receive as argument.
    Code:
    void getOnePerson(Person *person)
    {
            char line[1024]; // place for 1024 characters
     
            scanf("%s", line);
            strncpy(person->name, line, NAME_SIZE); // strncpy don't ensure that there is a terminator
            person->name[NAME_SIZE - 1] = '\0'; // therefore we set a terminator explicit in the last position
    …

    This is only quick & dirty but should give you ideas how you can populate the structure.
    Other have classes, we are class

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Woodstokk, I would never have guessed English wasn't your native language; it is very good. This gave me what I needed to get moving in the right direction. I should be able to complete it from here.

    Thank you so much for your quick response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming - Structures
    By mirchi.mike in forum C Programming
    Replies: 2
    Last Post: 06-08-2012, 05:52 AM
  2. Replies: 4
    Last Post: 04-09-2012, 05:44 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Replies: 1
    Last Post: 08-01-2002, 08:26 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread