[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Passing Structures to functions

Posted on 2005-04-28
Medium Priority
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

Expert Comment

ID: 13892204
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

efn earned 2000 total points
ID: 13892263
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);

Expert Comment

ID: 13892829

<Source code deleted>

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


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

873 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