Sinisterly
Using Structure as Function Arguements [C] - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Using Structure as Function Arguements [C] (/Thread-Using-Structure-as-Function-Arguements-C)



Using Structure as Function Arguements [C] - Boomslang - 06-24-2013

So.. I'm making a game with a text interface (like the one that Sheldon played in "The Big Bang Theory").
I used structures to declare weapons and characters. Source code is like this;

Code:
#include <stdio.h> typedef struct{ char *name; int atk; int bonus; }weapon[10]; typedef struct{ char *name; int atk; int hp; int boss; char *equipped; }character[10]; int main(void){ //Declaring weapons weapon weapons; weapons[0].name = "LongSword"; weapons[0].atk = 10; weapons[0].bonus = 0; weapons[1].name = "Axe"; weapons[1].atk = 11; weapons[1].bonus = 0; weapons[2].name = "Dagger"; weapons[2].atk = 7; weapons[2].bonus = 0; weapons[3].name = "Katana"; weapons[3].atk = 20; weapons[3].bonus = 1; weapons[4].name = "Gladius"; weapons[4].atk = 8; weapons[4].bonus = 0; weapons[5].name = "Spear"; weapons[5].atk = 12; weapons[5].bonus = 0; weapons[6].name = "BroadSword"; weapons[6].atk = 11; weapons[6].bonus = 0; weapons[7].name = "Glaive"; weapons[7].atk = 9; weapons[7].bonus = 0; //Declaring characters character chars; chars[0].name = "Troll"; chars[0].atk = 20; chars[0].hp = 20; chars[0].boss = 0; chars[1].name = "Knight"; chars[1].atk = 10; chars[1].hp = 50; chars[1].boss = 0; chars[2].name = "Goliath"; chars[2].atk = 50; chars[2].hp = 70; chars[2].boss = 0; chars[3].name = "Werewolf"; chars[3].atk = 30; chars[3].hp = 30; chars[3].boss = 0; chars[4].name = "Red Dragon"; chars[4].atk = 50; chars[4].hp = 10; chars[4].boss = 1; chars[5].name = "Me"; chars[5].atk = 5; chars[5].hp = 100; chars[5].boss = 0; //End declaring return 0; }

It'd be more useful if I could just pass structure to a function as parameter. As you can see I'd just call a function to make declarings and stuff.. So how to do this? Also feel free to suggest anything about the game.
Thank you Smile


Using Structure as Function Arguements [C] - Boomslang - 06-24-2013

So.. I'm making a game with a text interface (like the one that Sheldon played in "The Big Bang Theory").
I used structures to declare weapons and characters. Source code is like this;

Code:
#include <stdio.h> typedef struct{ char *name; int atk; int bonus; }weapon[10]; typedef struct{ char *name; int atk; int hp; int boss; char *equipped; }character[10]; int main(void){ //Declaring weapons weapon weapons; weapons[0].name = "LongSword"; weapons[0].atk = 10; weapons[0].bonus = 0; weapons[1].name = "Axe"; weapons[1].atk = 11; weapons[1].bonus = 0; weapons[2].name = "Dagger"; weapons[2].atk = 7; weapons[2].bonus = 0; weapons[3].name = "Katana"; weapons[3].atk = 20; weapons[3].bonus = 1; weapons[4].name = "Gladius"; weapons[4].atk = 8; weapons[4].bonus = 0; weapons[5].name = "Spear"; weapons[5].atk = 12; weapons[5].bonus = 0; weapons[6].name = "BroadSword"; weapons[6].atk = 11; weapons[6].bonus = 0; weapons[7].name = "Glaive"; weapons[7].atk = 9; weapons[7].bonus = 0; //Declaring characters character chars; chars[0].name = "Troll"; chars[0].atk = 20; chars[0].hp = 20; chars[0].boss = 0; chars[1].name = "Knight"; chars[1].atk = 10; chars[1].hp = 50; chars[1].boss = 0; chars[2].name = "Goliath"; chars[2].atk = 50; chars[2].hp = 70; chars[2].boss = 0; chars[3].name = "Werewolf"; chars[3].atk = 30; chars[3].hp = 30; chars[3].boss = 0; chars[4].name = "Red Dragon"; chars[4].atk = 50; chars[4].hp = 10; chars[4].boss = 1; chars[5].name = "Me"; chars[5].atk = 5; chars[5].hp = 100; chars[5].boss = 0; //End declaring return 0; }

It'd be more useful if I could just pass structure to a function as parameter. As you can see I'd just call a function to make declarings and stuff.. So how to do this? Also feel free to suggest anything about the game.
Thank you Smile


RE: Using Structure as Function Arguements [C] - Deque - 06-25-2013

Example code:

Code:
#include <stdio.h> struct samplestruct{ char* name; }; void printstruct(struct samplestruct sample); struct samplestruct declarestruct(); int main() { struct samplestruct sample = declarestruct(); printstruct(sample); return 0; } void printstruct(struct samplestruct sample) { printf("%s\n", sample.name); } struct samplestruct declarestruct() { struct samplestruct sample; sample.name = "test"; return sample; }