尝试学习C所以我做了这个简短的程序来尝试和模仿类。这个代码有什么我不应该用C做的,或者我可以用任何方式改进它,比如最佳做法或其他什么?
structs.h
struct weapon {
char name[30];
int damage;
int durability;
};
struct player {
int health;
int armor_level;
int currency;
struct weapon player_weap;
};
的main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
struct player new_player();
void main() {
struct player user = new_player();
printf("The players health is %d.\n", user.health);
printf("The players armor is %d.\n", user.armor_level);
printf("The players currency is %d.\n", user.currency);
printf("The players weapon is the %s.\n", user.player_weap.name);
printf("The players weapon does %d damage and has %d durability.\n", user.player_weap.damage, user.player_weap.durability);
}
struct player new_player() {
struct player new_player;
new_player.health = 100;
new_player.armor_level = 0;
new_player.currency = 0;
strcpy(new_player.player_weap.name, "Starter Sword");
new_player.player_weap.damage = 1;
new_player.player_weap.durability = 100;
return new_player;
}
答案 0 :(得分:3)
Well there are few points:- In case the structure is not too big you can do it in this way. It is perfectly correct.
malloc
is computationally more costlier than copying variables on return from a function. In case the structure size is bigger then you should malloc
it. That way you won't end up using a huge amount of memory in stack.
You should try to think of making the code reusable. You can keep a function which will print a structure. Something like (the same reason you kept a seperate function for getting an instance of struct).
void printPlayer(struct player p){
...
}
That way any time you try to print it - you would just call it. This saves you from repeating the same code of printing multiple times.
Another thing is, you can typedef
the struct player
to something more readable. In this case player
is itself readable but in some cases typedef
helps. But well that's debatable.
Also the main()
should be (the OS expects an integer back)
int main(void){
..
return 0;
}
You can move the declaration of the function printPlayer
or new_player
to the header itself. That makes much more sense.
One another thing is using suitable names. structs.h
is not a good name for a header file. In this project there won't be any struct
header file? Keep the name Player.h
or Game.h
. That is much more readable.
Use header guards
#ifndef PLAYER_H
#define PLAYER_H
struct weapon {
char name[30];
int damage;
int durability;
};
struct player {
int health;
int armor_level;
int currency;
struct weapon player_weap;
};
#endif
答案 1 :(得分:2)
It's just a matter of preference, but when I've used structs for things like that I've always done typedef
to give myself a new data type. It makes writing the code easier, and helps me better comprehend what it is happening.
typedef struct {
char name[30];
int damage;
int durability;
} Weapon;
typedef struct {
int health;
int armor_level;
int currency;
Weapon player_weap;
} Player;
So instead of declaring a function like
struct player new_player();
You would do it like this
Player new_player();
It simulates the process of working with classes much better, IMO. Also, whoever told you to use void main()
is horribly out of touch. Don't ever use that unless you're writing a kernel or a microcontroller or something. It's int main(void)
for pretty much every programming environment. Make sure you return 0;
if your program has finished running successfully.