Last active
August 29, 2015 14:07
-
-
Save AdnoC/ea71f5e95a475339ca90 to your computer and use it in GitHub Desktop.
/r/learnprogramming post part 3
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<iostream> | |
using namespace std; | |
// I had three types of structs. One contained another. That one contained an array of an other as well as a pointer to one. | |
struct Bar { | |
int value1; | |
}; | |
struct Foo { | |
Bar bars[5]; | |
Bar *next; | |
}; | |
struct Foobar { | |
Foo foo; | |
}; | |
// I also had a function that initialized them. | |
void Bar_initialize(Bar *bar, int v1) { | |
bar->value1 = v1; | |
} | |
void Foo_initialize(Foo *foo) { | |
for(int i = 0; i < 5; i++) { | |
Bar_initialize(foo->bars + i, i); | |
} | |
// Create a new Bar in this function and set next to point to it | |
Bar b; | |
Bar_initialize(&b, 4); | |
foo->next = &b; | |
} | |
// Fixed Foobar_initialize function from the last gist. | |
void Foobar_initialize(Foobar *fb) { | |
Foo_initialize(&fb->foo); | |
} | |
// There were also other functions that did stuff with the structs | |
void print_foobar(Foobar *fb) { | |
// Only looking at the next, not the things in bars | |
cout << fb->foo.next->value1 << endl; | |
} | |
void do_something(Foobar *fb) { | |
print_foobar(fb); | |
} | |
int main() { | |
Foobar fb; | |
Foobar_initialize(&fb); | |
do_something(&fb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment