[prog] C language: structures containing structures

aec brat at magma.ca
Thu Nov 4 06:33:18 EST 2004


I understand structures that contain structures, for instance: 

struct time
{
  int hour;
  int minutes;
  int seconds;
};

struct date
{
  int month;
  int day;
  int year;
};

struct dateAndTime
{
  struct time sTime;
  struct date sDate;
};

So now i want to write a silly function that adds 1 to everything in
my structures just to test that passing the values back and forth
through the function work properly.

// test function to try to pass and return a structure
struct dateAndTime timekeeper(struct dateAndTime current) 
{
  struct dateAndTime new;
  new.sDate.month = current.sDate.month +1 ;
  new.sDate.day = current.sDate.day+ 1;
  new.sDate.year = current.sDate.year + 1 ;
  new.sTime.hour = current.sTime.hour + 1;
  new.sTime.minutes = current.sTime.minutes + 1;
  new.sTime.seconds = current.sTime.seconds + 1;

  return new;
}

int main(void)
{
  struct dateAndTime today =
  { {2,1,2004} , {3,30,0} };

  printf("%i/%i/%i\t", today.sDate.month, today.sDate.day, today.sDate.year);
  printf("%i:%i:%i\n", today.sTime.hour, today.sTime.minutes, today.sTime.seconds);

  struct dateAndTime later =
  { {0,0,0} , {0,0,0} };

  later.sDate = timekeeper(later.sDate);
  later.sTime = timekeeper(later.sTime);
  printf("%i/%i/%i\t", later.sDate.month, later.sDate.day, later.sDate.year);
  printf("%i:%i:%i\n", later.sTime.hour, later.sTime.minutes, later.sTime.seconds);


  return 0;
}

So I get a compiler error:

ex.9.5.c:51: error: incompatible type for argument 1 of `timekeeper'

line 51 is: 

later.sDate = timekeeper(later.sDate); 

So i am using the wrong syntax to pass the structure to my function or, my function
is set up incorrectly to recive my structures?

This is topic is not clealy discussed in my C book so Im kinda
confused at the moment. 

-- 
Angelina Carlton


More information about the Programming mailing list