Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
typedef struct {
char *eyes;
char *hair;
} Colors;
typedef struct {
char *name;
char *age;
Colors colors;
} Person;
int main(int argc, const char *argv[])
{
Person person = {"James Newton", 21, {"brown", "brown"}};
printf("Name: %s, Age: %i, Eye color: %s, Hair color: %s\n", person.name, person.age, person.colors.eyes, person.colors.hair);
return 0;
}
class Person
attr_accessor :name, :age, :colors
def initialize(name, age, eye_color, hair_color)
self.name = name
self.age = age
self.colors = Colors.new(eye_color, hair_color)
end
end
class Colors
attr_accessor :eyes, :hair
def initialize(eye_color, hair_color)
self.eyes = eye_color
self.hair = hair_color
end
end
person = Person.new('James Newton', 21, 'brown', 'brown')
person.name #=> "James Newton"
person.colors.hair #=> "brown"
people = []
people << Person.new('James Newton', 21, 'brown', 'brown')
# add more people, like Robert and Zach.
people.first.name #=> "James Newton"
#include <stdio.h>
int main(int argc, const char *argv[])
{
char *people[3] = {"James Newton", "Robert Babcock", "Zach Smith"};
for (int i = 0; i < sizeof(people) / sizeof(people[0]); i++) {
printf("Name: %s\n", people[i]);
}
return 0;
}
#include <stdio.h>
typedef struct {
char *name;
int age;
} Person;
int main(int argc, const char *argv[])
{
Person people[2] = {{"James Newton", 21}, {"Robert Babcock", 22}};
for (int i = 0; i < sizeof(people) / sizeof(Person); i++) {
printf("Name: %s, Age: %i\n", people[i].name, people[i].age);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment