15,440
社区成员
发帖
与我相关
我的任务
分享
typedef struct tagProperty
{
char Name[64];
char Data[64];
}Property;
typedef struct tagItem
{
Property property1;
void *arr[19]; //最多可以添加19个新成员,合起来一共20个
}Item;
int main()
{
Item item;
item.arr = (Property *)("白菜2号", "是个大笨蛋!");
item.arr = (Property *)("白菜3号", "是个大笨蛋!");
item.arr = (Property *)("白菜4号", "是个大笨蛋!");
item.arr = (Property *)("白菜5号", "是个大笨蛋!");
item.arr = (Property *)("白菜6号", "是个大笨蛋!");
return 0;
}
void Itemcpy(Item *ItemTo, Item *ItemSource)
{
int i = 0;
for (i = 1; i <= 20; i++)
{
if ((*ItemSource).arr[i - 1] != NULL)
{
(*ItemTo).arr[i - 1] = (void *)malloc(sizeof(Property));
strcpy(((Property *)((*ItemTo).arr[i - 1]))->Data, ((Property *)((*ItemSource).arr[i - 1]))->Data);
strcpy(((Property *)((*ItemTo).arr[i - 1]))->Name, ((Property *)((*ItemSource).arr[i - 1]))->Name);
strcpy(((Property *)((*ItemTo).arr[i - 1]))->Type, ((Property *)((*ItemSource).arr[i - 1]))->Type);
}
}
}
#pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
typedef struct tagProperty
{
char Name[64];
char Data[64];
}Property;
typedef struct tagItem
{
Property property1;
void *arr[19]; //最多可以添加19个新成员,合起来一共20个
}Item;
int main()
{
Item item;
int i;
for (i = 0; i < 19; i++) item.arr[i] = NULL;
strcpy(item.property1.Name, "白菜1号");
strcpy(item.property1.Data, "是个大笨蛋!");
item.arr[0] = (void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[0]))->Name, "白菜2号");
strcpy(((Property *)(item.arr[0]))->Data, "是个大笨蛋!");
item.arr[1] = (void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[1]))->Name, "白菜3号");
strcpy(((Property *)(item.arr[1]))->Data, "是个大笨蛋!");
item.arr[2] = (void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[2]))->Name, "白菜4号");
strcpy(((Property *)(item.arr[2]))->Data, "是个大笨蛋!");
for (i = 0; i < 20; i++) {
if (i == 0) {
printf("%d,%s,%s\n", i, item.property1.Name, item.property1.Data);
}
else {
if (NULL != item.arr[i - 1]) {
printf("%d,%s,%s\n", i, ((Property *)(item.arr[i - 1]))->Name, ((Property *)(item.arr[i - 1]))->Data);
}
}
}
Item temp_item;
temp_item = item;
strcpy(temp_item.property1.Name, "白菜999号");
strcpy(((Property *)(temp_item.arr[0]))->Name, "白菜888号");
for (i = 0; i < 20; i++) {
if (i == 0) {
printf("%d,%s,%s\n", i, item.property1.Name, item.property1.Data);
}
else {
if (NULL != item.arr[i - 1]) {
printf("%d,%s,%s\n", i, ((Property *)(item.arr[i - 1]))->Name, ((Property *)(item.arr[i - 1]))->Data);
}
}
}
for (i = 0; i < 19; i++) {
if (NULL != item.arr[i]) {
free(item.arr[i]); item.arr[i] = NULL;
}
}
return 0;
}
//0,白菜1号,是个大笨蛋!
//1,白菜2号,是个大笨蛋!
//2,白菜3号,是个大笨蛋!
//3,白菜4号,是个大笨蛋!
//0,白菜1号, 是个大笨蛋!
//1,白菜888号, 是个大笨蛋!
//2,白菜3号, 是个大笨蛋!
//3,白菜4号, 是个大笨蛋!
#pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
typedef struct tagProperty
{
char Name[64];
char Data[64];
}Property;
typedef struct tagItem
{
Property property1;
void *arr[19]; //最多可以添加19个新成员,合起来一共20个
}Item;
int main()
{
Item item;
int i;
for (i=0;i<19;i++) item.arr[i]=NULL;
strcpy(item.property1.Name,"白菜1号");
strcpy(item.property1.Data,"是个大笨蛋!");
item.arr[0]=(void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[0]))->Name,"白菜2号");
strcpy(((Property *)(item.arr[0]))->Data,"是个大笨蛋!");
item.arr[1]=(void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[1]))->Name,"白菜3号");
strcpy(((Property *)(item.arr[1]))->Data,"是个大笨蛋!");
item.arr[2]=(void *)malloc(sizeof(Property));
strcpy(((Property *)(item.arr[2]))->Name,"白菜4号");
strcpy(((Property *)(item.arr[2]))->Data,"是个大笨蛋!");
for (i=0;i<20;i++) {
if (i==0) {
printf("%d,%s,%s\n",i,item.property1.Name,item.property1.Data);
} else {
if (NULL!=item.arr[i-1]) {
printf("%d,%s,%s\n",i,((Property *)(item.arr[i-1]))->Name,((Property *)(item.arr[i-1]))->Data);
}
}
}
for (i=0;i<19;i++) {
if (NULL!=item.arr[i]) {
free(item.arr[i]);item.arr[i]=NULL;
}
}
return 0;
}
//0,白菜1号,是个大笨蛋!
//1,白菜2号,是个大笨蛋!
//2,白菜3号,是个大笨蛋!
//3,白菜4号,是个大笨蛋!
//