Please help with structs.

This is a discussion on Please help with structs. within the C Programming forums, part of the General Programming Boards category; Ok, I have to write a program to sort a list pf persons by date of birth. The problems I ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Please help with structs.

    Ok, I have to write a program to sort a list pf persons by date of birth. The problems I am have are numerous but will try to be brief. My "person.h" file is as follows:
    Code:
    #ifndef _PERSON_H
    #define _PERSON_H
    
    #include <stdio.h>
    
    typedef struct {
    	int month;
    	int day;
    	int year;
    } date;
    
    typedef struct {
    	char lastName[25];
    	char firstName[25];
    	char middleInitial[1];
    	date dob;
    } person;
    
    #define N 100
    #define FALSE 0
    #define TRUE 1
    
    int getInfo (person array[], int num_elements)
    
    #endif
    The biggest problem I have is how do I pass the struct info to a seperate function? I need to have the following functions: getInfo.c, selectionSort.c, printList.c, swap.c and probably a reverse.c and findMin.c. This is driving me crazy and all the pages i have read do not do any justice at all. Can anyone help?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Since you are going to pass an array of structs to the function I would start by making a "createPerson" function that returns a person. You can then call this function in a loop to fill up an array of persons. So your array will be an array of pointers to person structs. You can declare it like this:

    Code:
    struct Person *p[size];
    
    // Edit: in your case it would be like this since you have a typedef in your struct definition.
    
    person *p[size];
    Then to pass it to a function:

    Code:
    sortPersonList(p, size);
    The prototype must take into account that it's an array of pointers so:

    Code:
    void sortPersonList(struct Person **p, int size);

    BTW, don't forget to free all person structs inside the array when your done.
    Last edited by Subsonics; 07-10-2010 at 07:10 PM.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Sorry but I am not following you there. I really do not want to create another function. Here is what I have so far and please, I have to use these functions.
    Code:
    /* person.h */
    #ifndef _PERSON_H
    #define _PERSON_H
    
    #include <stdio.h>
    
    typedef struct {
    	int month;
    	int day;
    	int year;
    } date;
    
    typedef struct {
    	char lastName[25];
    	char firstName[25];
    	char middleInitial[1];
    	date dob;
    } person;
    
    #define N 100
    #define FALSE 0
    #define TRUE 1
    
    int getInfo (person array[], int num_elements);
    int isInOrder (person x[], int num_elements);
    void ssort (person x[], int num_elements);
    void swap (person * firstElement, person * secondElement);
    
    #endif
    
    /* main.c */
    #include "person.h"
    
    int main()
    {
    	person x[N];
    	int num_elements;
    	
    	num_elements = getInfo (x, N);
    	
    	if (isInOrder (x, num_elements)) printf ("%s %s %s %d %d %d\n", x[3].firstName, x[3].middleInitial, x[3].lastName, x[3].dob.month,
    			x[3].dob.day, x[3].dob.year);
    			
    			else ssort (x, num_elements);
    	
    	return 0;
    }
    
    /* getInfo.c */
    #include "person.h"
    
    int getInfo (person array[], int num_elements)
    {
    	int count = 0;
    	while (count < num_elements)
    	{
    		if (scanf("%s%s%s%d%d%d", array[count].firstName, &array[count].middleInitial, &array[count].lastName,
    				&array[count].dob.month, &array[count].dob.day, &array[count].dob.year) == EOF) break;
    				count++;
    	}
    	return count;
    }
    
    /* isInOrder.c */
    #include "person.h"
    
    int isInOrder (person x[], int num_elements)
    {
    	int counter = 0;
    	while (counter < num_elements - 1)
    	{
    		if (x[counter] > x[counter + 1]) return FALSE;
    		counter++;
    	}
    	
    	return TRUE;
    	
    }
    
    /* ssort.c */
    #include "person.h"
    
    void ssort (person x[], int num_elements)
    {
    	int counter;
    	
    	while (num_elements--)
    	{
    		counter = findMin (x, num_elements);
    		swap (&x[counter], &x[num_elements]);
    	}
    }
    
    /* swap.c */
    void swap (person * firstElement, person * secondElement)
    {
    	int tempElement = *firstElement;
    	*firstElement = *secondElement;
    	*secondElement = tempElement;
    }
    I realize I am missing a couple of functions, namely findMax (or findMin) and probably a reverse.c function. What am I doing wrong here? Thanks!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But it's hard for me to guess. Based on your first question it was not clear if your functions existed or not, neither what they were suppose to do. So, I told you how I would approach it.

    But what is it that does not work in your current program?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Since I was not making myself clear, here is the full program. ANYONE tell me what is wrong and how to correct it. I would really appreciate it. If you can show me some code examples. That would really help.
    Code:
    /* person.h */
    #ifndef _PERSON_H
    #define _PERSON_H
    
    #include <stdio.h>
    
    typedef struct {
    	int month;
    	int day;
    	int year;
    } date;
    
    typedef struct {
    	char lastName[25];
    	char firstName[25];
    	char middleInitial[1];
    	date dob;
    } person;
    
    #define N 100
    #define FALSE 0
    #define TRUE 1
    
    int getInfo (person array[], int num_elements);
    void ssort (person x[], int count);
    void reverse (person x[], int count);
    void printList (person x[], int num_elements);
    int isInOrder (person x[], int num_elements);
    int findmax (person x[], int count);
    void swap (person * firstElement, person * secondElement);
    
    #endif
    
    /* main.c */
    #include "person.h"
    
    int main()
    {
    	person x[N];
    	int count;
    	
    	count = getInfo (x, N);
    	
    	if (isInOrder (x, num_elements)) printf ("\nIts in order. \n");
    			else ssort (x, num_elements);
    			
    	reverse(x, num_elements);
    	printList (x, num_elements);
    	
    	return 0;
    }
    
    /* the input file, i am compiling on  a unix machine */
    smith 3 10 1956
    johnson 4 2 1970
    sharp 6 14 1964
    sharp 5 9 2000
    marion 4 3 1900
    
    /* getInfo.c function */
    #include "person.h"
    
    int getInfo (person array[], int num_elements)
    {
    	int count = 0;
    	while (count < num_elements)
    	{
    		if (scanf("%s%s%s%d%d%d", array[count].lastName, &array[count].dob.month, &array[count].dob.day, 
    								  &array[count].dob.year) == EOF) break;
    				count++;
    	}
    	return count;
    }
    
    /* isInOrder.c function */
    #include "person.h"
    
    int isInOrder (person x[], int num_elements)
    {
    	int counter = 0;
    	while (counter < num_elements - 1)
    	{
    		if (x[counter] > x[counter + 1]) return FALSE;
    		counter++;
    	}
    	
    	return TRUE;
    	
    }
    
    /* ssort.c selection sort function */
    #include "person.h"
    
    void ssort (person x[], int count)
    {
    	int counter;
    	
    	while (count--)
    	{
    		counter = findmax (x, count);
    		swap (&x[counter], &x[count]);
    	}
    }
    
    /* swap.c function */
    void swap (person * firstElement, person * secondElement)
    {
    	int tempElement = *firstElement;
    	*firstElement = *secondElement;
    	*secondElement = tempElement;
    }
    
    /* findmax.c function */
    #include "person.h"
    
    int findmax (person x[], int count)
    {
    	int maxind = 0;
    	int counter = 1;
    	while (counter <= count)
    	{
    		if (x[counter] > x[maxind]) maxind = counter;
    		counter++;
    	}
    	return maxind;
    }
    
    /* reverse.c function */
    #include "person.h"
    
    void reverse (person x[], int count)
    {
    	int * p = &x[0];
    	int * q = p + count - 1;
    	
    	while (p < q)
    	{
    		swap (p,q);
    		p++;
    		q--;
    	}
    }
    
    /* printList.c function */
    #include "person.h"
    
    void printList (person x[], int num_elements)
    {
    	int * p = &x[0];
    	printf("\n");
    	while (p <= &x[num_elements - 1])
    	{
    		printf (%s %d\n" *p);
    		p++;
    	}
    }
    There is too many compile errors to list. The point I am trying to make is am I passing the elements of the struct correctly? Obviously I am not or it would compile with no errors and run. If you can some rework of at least on of my functions would hlep with the correct code. Thank You all for looking and helping.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Since you don't use pointers, but static memory you should be able to pass it like any array. I'll make a general example, hope that's ok.

    Code:
    person p[size];
    
    someFunction(p, size);
    What are the first error you get? They have a tendency to cascade into many.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by csharp100 View Post
    There is too many compile errors to list. The point I am trying to make is am I passing the elements of the struct correctly? Obviously I am not or it would compile with no errors and run. If you can some rework of at least on of my functions would hlep with the correct code. Thank You all for looking and helping.
    It's much easier with some error messages to go by, at least the first few. You generally get information on what type of error it is and on what line, instead of trying to find it manually.
    Last edited by Subsonics; 07-10-2010 at 10:00 PM. Reason: Missed the conditional pre processor directives, duh. :)

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Errors I got doing the OP's job:

    Code:
    gcc -Wall -pedantic -o test *.c
    findmax.c: In function ‘findmax’:
    findmax.c:10: error: invalid operands to binary > (have ‘person’ and ‘person’)
    getInfo.c: In function ‘getInfo’:
    getInfo.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int *’
    getInfo.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘int *’
    getInfo.c:10: warning: too few arguments for format
    isInOrder.c: In function ‘isInOrder’:
    isInOrder.c:9: error: invalid operands to binary > (have ‘person’ and ‘person’)
    main.c: In function ‘main’:
    main.c:11: error: ‘num_elements’ undeclared (first use in this function)
    main.c:11: error: (Each undeclared identifier is reported only once
    main.c:11: error: for each function it appears in.)
    printList.c: In function ‘printList’:
    printList.c:6: warning: initialization from incompatible pointer type
    printList.c:8: warning: comparison of distinct pointer types lacks a cast
    printList.c:10: error: invalid operands to binary * (have ‘char *’ and ‘int *’)
    reverse.c: In function ‘reverse’:
    reverse.c:6: warning: initialization from incompatible pointer type
    reverse.c:11: warning: passing argument 1 of ‘swap’ from incompatible pointer type
    person.h:30: note: expected ‘struct person *’ but argument is of type ‘int *’
    reverse.c:11: warning: passing argument 2 of ‘swap’ from incompatible pointer type
    person.h:30: note: expected ‘struct person *’ but argument is of type ‘int *’
    swap.c:2: error: expected ‘)’ before ‘*’ token
    The last one is "fixed" by including person.h there as well, but you can't treat persons like they're ints. The same is true in findmax and isInOrder. It looks like you found some code to do the tasks you want to do, but you don't know how to adapt it to your person struct. So you need to consider what makes a person "greater than" or "less than" another person...that's the element of the struct which must be compared against the others in these tasks.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Thank you for all your help, I figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 02:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

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