試圖學習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