试图学习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;
}
如果您还没有一个具体的问题,只是想在你的代码的一些意见比它是更好地使用https://codereview.stackexchange.com/ – taskinoor
我知道你是不会因为内存而拥挤,但是还有一些可以改变结构在内存中排列的东西。结构填充和包装? – WedaPashi