UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » A Simple Program

#1 2007-05-03 11:56 AM

aaronc
Member
Registered: 2007-04-25
Posts: 8

Re: A Simple Program

Hi Every1, I am trying to run the following simple program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct employeeRecord
{
   char surname[20];
   double hourlyRate;
   int empNumber;
};
typedef struct employeeRecord employee;

void fillEmployee(employee emp, char *name, double rate, int num);
void swap(employee employees[], int a, int b);
void sortEmployees(employee employees[], int number);
void printEmployees(employee employees[], int number);
   
int main()
{
   employee employees[5];
 
   fillEmployee(employees[0], "&Stone", 35.75, 053);
   fillEmployee(employees[1], "&Rubble", 12.00, 163);
   fillEmployee(employees[2], "&Flintstone", 15.75, 97);
   fillEmployee(employees[3], "&Pebble", 10.25, 104);
   fillEmployee(employees[4], "&Rockwall", 22.75, 15); 
         
   printEmployees(employees, 5);
   sortEmployees(employees, 5);
   printEmployees(employees, 5);
       
   return 0;
}

void fillEmployee(employee emp, char *name, double rate, int num)
{
   strcpy(emp.surname, name);
   emp.hourlyRate = rate;
   emp.empNumber = num;
}
void printEmployees(employee employees[], int number)
{
   int i;
     
   for (i=0; i<number; i++)
   {
      printf("%-20s%4d   %.2f\n", employees[i].surname,
         employees[i].empNumber, employees[i].hourlyRate);
   }
   printf("\n");
}
       
void swap(employee employees[], int a, int b)
{
   employee temp = employees[a];
   employees[a]  = employees[b];
   employees[b]  = temp;

   /* code to swap employee a and b around in employees */
}   

void sortEmployees(employee employees[], int number)
{
   /* use selection sort to order employees,
      in alphabetical employee name order
   */
   int inner, outer, max;

   for (outer=number-1; outer>0; outer--)
   {
      max = 0;  
      for (inner=1; inner<=outer; inner++)
      {
         if (strcmp(employees[inner].surname, employees[max].surname) < 0)
            max = inner;
      }
      if (max != outer)
      {
         swap(employees, max, outer);
      }
   }
}

but when I run it, I don't get the correct output. I get all sorts of weird numbers. Please help.

Offline

#2 2007-05-03 12:12 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: A Simple Program

If you want to change an object(struct, or variable) in a function you must pass a reference or pointer.

Offline

#3 2007-05-03 12:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: A Simple Program

Yes, what yurec said...  Specifically, you want to change fillEmployee() like so:

void fillEmployee(employee *emp, char *name, double rate, int num)
{
   strcpy(emp->surname, name);
   emp->hourlyRate = rate;
   emp->empNumber = num;
}

And, change all calls to it to pass pointers to each struct instead of the struct itself...
The other functions are ok, as they already effectively get a pointer, since you pass
the entire array of structs to them...

Offline

  • Index
  • » C
  • » A Simple Program

Board footer

Powered by FluxBB