#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
struct sanguo
{
char * country; //国家
char * name; //名字
int * force; //武力
int * Intelligence; //智力
int len;
int cnt;
};
void init_arr(struct sanguo *pArr, int length);
void show_arr(struct sanguo *pArr);
bool is_empty(struct sanguo *pArr);
bool append_arr(struct sanguo *pArr, char val1, char val2, int val3, int val4);
int main(void)
{
struct sanguo arr;
init_arr(&arr, 50); //创建一个人物基本信息的数组
show_arr(&arr);
append_arr(&arr, '哈', 'z', 225, 200);
show_arr(&arr);
return 0;
}
void init_arr(struct sanguo *pArr, int length)
{
pArr->country = (char*)malloc(sizeof(char)*length);
pArr->name = (char*)malloc(sizeof(char)*length);
pArr->force = (int*)malloc(sizeof(int)*length);
pArr->Intelligence = (int*)malloc(sizeof(int)*length);
if ((NULL == pArr->country)||(NULL == pArr->name)||(NULL == pArr->force)||(NULL == pArr->Intelligence))
{
printf("动态内存分配失败\n");
exit(-1);
}
//设定 数组的元素个数为0 因为目前没有添加 长度 为MAIN函数中设定
else
{
pArr->cnt = 0;
pArr->len = length;
}
return ;
}
void show_arr(struct sanguo *pArr)
{
if (is_empty(pArr))
{
printf("数组为空\n");
}
else
{
for(int i=0; i<pArr->cnt; ++i)
{
printf("国家: %c\0", pArr->country[i]);
printf("姓名: %c\0", pArr->name[i]);
printf("武力值: %d", pArr->force[i]);
printf("智力值: %d", pArr->Intelligence[i]);
printf("\n");
}
}
}
bool append_arr(struct sanguo *pArr, char val1, char val2, int val3, int val4)
{
if (is_full(pArr))
return false;
else
{
pArr->country[pArr->cnt] /*此处一定要有一个pArr->cnt的下标*/ = val1;
pArr->name[pArr->cnt] = val2;
pArr->force[pArr->cnt] = val3;
pArr->Intelligence[pArr->cnt] = val4;
pArr->cnt++;
return true;
}
}
字数打不下 我把几个不重要的函数去掉了 请忽略这个问题
我只想知道 怎么输出中文啊 !!
输出结果是:
数组为空
国家: ;彰? z武力值: 225智力值: 200
#include <stdio.h>