Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Created February 17, 2016 21:23
Show Gist options
  • Select an option

  • Save guyhughes/e58d91385b72d0ef8a63 to your computer and use it in GitHub Desktop.

Select an option

Save guyhughes/e58d91385b72d0ef8a63 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
struct car{
char make[30];
char model[30];
int year;
int mileage;
};
struct carlist {
int num_cars;
struct car *cars;
};
void print_car(struct car c)
{
printf("Make:\t%s\nModel:\t%s\nYear:\t%d\n Mileage:\t%d\n", c.make,c.model,c.year,c.mileage);
}
void print_car_list(struct carlist l)
{
int i;
for(i=0; i < l.num_cars; ++i){
print_car(l.cars[i]);
}
}
void add_car_to_list(struct carlist *l, struct car c)
{
// check space in array
if ( sizeof(l->cars)/sizeof(l->cars[0]) <= l->num_cars ){
printf("There are too many cars in the array. Unable to add an element.\n");
}
int i = l->num_cars;
l->cars[i]=c;
++l->num_cars;
}
struct carlist *find_cars(int year, struct carlist *l)
{
struct carlist *result = calloc(1,sizeof(struct carlist) );
int i;
for(i=0; i < l->num_cars; ++i){
if (l->cars[i].year == year){
add_car_to_list(result,l->cars[i]);
}
}
return result;
}
int main ( void )
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment