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