Last active
August 29, 2015 14:07
-
-
Save AdnoC/6384b7f6cc55f03e5d4e to your computer and use it in GitHub Desktop.
/r/learnprogramming post part 2
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); | |
} | |
foo->next = foo->bars; | |
} | |
void Foobar_initialize(Foobar *fb) { | |
// Passing the Foo in fb directly to the initialize function fixes the issue | |
Foo_initialize(&fb->foo); | |
} | |
// There were also other functions that did stuff with the structs | |
void print_foobar(Foobar *fb) { | |
for(int i = 0; i < 5; i++) { | |
cout << fb->foo.next->value1 << endl; | |
fb->foo.next++; | |
} | |
} | |
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