Passing Structures to functions

Posted on 2005-04-28
Last Modified: 2010-04-02
I am working on a school project and I am having a difficult time getting my structure to pass to my sort function.  I had it working, but in order to sort it, I had to make an array out date[10] to store all of them to be sorted.  But, the sort function doesn't accept them.  Need help fixing the parameters.  There is no compile error the way that it is(Atleast on Sun systems)Basically, what this program does is it reads in info from file

7 4 1776 Declaration of Independance
6 6 1944 D-day

It sorts these inputs by year, then month, then day.  I havn't completed the whole sorting part yet.  I know I am using some wrong parameters for passing the structure but I cant figure it out.  Here is the whole file:

/*  Name:       Jordan honeycutt
*   Project:    3
#include <stdio.h>
typedef struct {
                int year;
                int month;
                int day;
                char event[60];
                        } dates;

char *m[] = {"Doesn't exist", "January", "February", "March", "April", "May",
     "June", "July", "August", "September", "October", "November", "December"};

int sort(dates date[10], int count);
int swap(dates *i, dates *j);

   dates date[10];
   int y,c=0;
   char line[120], *end;
   while((end = gets(line)) !=0)

           sscanf(line, "%d %d %d", &date[c].month, &date[c].day, &date[c].year);
           strcpy(date[c].event, &line[11]);
           printf("%s      \t%d\t%d\t%s\n", m[date[c].month], date[c].day, date[c].year, date[c].event);
  printf("%d %d %d %d\n", date[0].year, date[1].year, date[2].year, date[2].year);
 /* the above state pritns out all the dates correctly, for now, I am just trying to do year
  * The parameters of the function call and the function itself are apparently not right.
  *  It prints perfectly here, but prints a 0 in the sort function.*/
  sort(&date[10], c);


sort(dates date[10], int count)
   int i,j;
printf("%d\n", date[1].year);   /* I have entered debugging print statements here, this one prints out a 0 */

           if(date[i].year > date[j].year)
           else if (date[i].year == date[j].year && date[i].month > date[j].month)
           else if (date[i].month == date[j].month && date[i].day > date[j].day)

swap(dates *i, dates *j)
   dates temp;
   temp.year = (*i).year;
   (*i).year = (*j).year;
   (*i).year = temp.year;
   printf("temp.year = %d, [i].year = %d, [j].year = %d\n", temp.year, (*i).year, (*j).year);  /* debugging statement  */

   /*strcpy(temp.event, a.event);  
   strcpy(b.event, temp.event);*/

I appreciate the help from anyone.  
Question by:inimicaljords
    LVL 5

    Expert Comment

    i believe if you use pass by value instead of using pointers... the sort program would turn out to be lot simple...

    i think i am a bit rusty about this concept , however i think C compiler does a shallow copy or a member by member copy when you assign  a structure like A = B so you are freed from dealing with the intrecacies of member by member copying...

    so instead of using a function call , you can simply use a temp structure to swap...

    LVL 15

    Accepted Solution

    date is an array of 10 dateses.  Arrays are numbered from zero, so the elements of date are date[0] through date[9].  date[10] is an element outside the bounds of the array, and &date[10] is a pointer to that element.

    sort(&date[10], c);

    So this passes the address of this out-of-bounds element as the first parameter.  Since the parameter is supposed to be an array of 10 dateses and date is exactly that, you should just pass it:

    sort(date, c);
    LVL 8

    Expert Comment


    <Source code deleted>

    Please refer to EE rules for providing full source solutions to homework problems.


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now