Created
May 9, 2017 06:20
-
-
Save andrewjbennett/ab9de1f828e9ab034b7f4b6c4e0ea70b to your computer and use it in GitHub Desktop.
example struct code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct student { | |
char name[100]; | |
int age; | |
}; | |
struct student make_student() { | |
struct student cur_student; | |
printf("1 What's your name?\n"); | |
scanf("%s", cur_student.name); | |
printf("What's your age?\n"); | |
scanf("%d", &cur_student.age); | |
// printf("Hello, %s! you're %d years old!\n", | |
// cur_student.name, cur_student.age); | |
return cur_student; | |
} | |
#define NUM_STUDENTS 3 | |
#define FAIL -1 | |
struct student find_by_age(int age, | |
struct student students[NUM_STUDENTS]) { | |
int i = 0; | |
while (i < NUM_STUDENTS) { | |
if (students[i].age == age) { | |
return students[i]; | |
} | |
i+=1; | |
} | |
exit(1); | |
} | |
void print_student(struct student cur) { | |
printf("Hello, %s! you're %d years old!\n", | |
cur.name, cur.age); | |
} | |
int main() { | |
// char name[100]; | |
// int age; | |
struct student all_students[NUM_STUDENTS]; | |
for (int i=0; i<NUM_STUDENTS; i++) { | |
all_students[i] = make_student(); | |
} | |
for (int i=0; i<NUM_STUDENTS; i++) { | |
print_student(all_students[i]); | |
} | |
int age_to_find; | |
printf("What age do you want to search for? "); | |
scanf("%d", &age_to_find); | |
struct student blah = find_by_age(age_to_find, all_students); | |
print_student(blah); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment