定义的数据是:
struct ns1__ArrayOfSeat
{
int __sizeSeat; /* sequence of elements <Seat> */
struct ns1__Seat *Seat; /* optional element of type ns1:Seat */
} seats;
struct ns1__Seat
{
char *Name; /* optional element of type xsd:string */
char *IDCard; /* optional element of type xsd:string */
char *Phone; /* optional element of type xsd:string */
};
现在有3组数据 {“测试一”,”0001″,”13505710001″} {“测试二”,”0002″,”13505710002″} {“测试三”,”0003″,”13505710003″}
问一下要怎么才能赋值到 seats中?
struct ns1__ArrayOfSeat
{
int __sizeSeat; /* sequence of elements <Seat> */
struct ns1__Seat *Seat; /* optional element of type ns1:Seat */
} seats;
struct ns1__Seat
{
char *Name; /* optional element of type xsd:string */
char *IDCard; /* optional element of type xsd:string */
char *Phone; /* optional element of type xsd:string */
};
现在有3组数据 {“测试一”,”0001″,”13505710001″} {“测试二”,”0002″,”13505710002″} {“测试三”,”0003″,”13505710003″}
问一下要怎么才能赋值到 seats中?
解决方案
20
简单写了下,可以这样赋值。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct ns1__ArrayOfSeat
{
int __sizeSeat; /* sequence of elements <Seat> */
struct ns1__Seat *Seat; /* optional element of type ns1:Seat */
} seats;
struct ns1__Seat
{
char *Name; /* optional element of type xsd:string */
char *IDCard; /* optional element of type xsd:string */
char *Phone; /* optional element of type xsd:string */
};
void main()
{
seats seatsArray[3] = {0};
seatsArray[0].Seat = (ns1__Seat *)malloc(sizeof(ns1__Seat));
seatsArray[1].Seat = (ns1__Seat *)malloc(sizeof(ns1__Seat));
seatsArray[2].Seat = (ns1__Seat *)malloc(sizeof(ns1__Seat));
seatsArray[0].Seat->IDCard = "测试1";
seatsArray[0].Seat->Name = "0001";
seatsArray[0].Seat->Phone = "13505710001";
seatsArray[1].Seat->IDCard = "测试2";
seatsArray[1].Seat->Name = "0002";
seatsArray[1].Seat->Phone = "13505710002";
seatsArray[2].Seat->IDCard = "测试3";
seatsArray[2].Seat->Name = "0003";
seatsArray[2].Seat->Phone = "13505710003";
printf("%s\t%s\t%s\n",seatsArray[0].Seat->IDCard,seatsArray[0].Seat->Name,seatsArray[0].Seat->Phone);
printf("%s\t%s\t%s\n",seatsArray[1].Seat->IDCard,seatsArray[1].Seat->Name,seatsArray[1].Seat->Phone);
printf("%s\t%s\t%s\n",seatsArray[2].Seat->IDCard,seatsArray[2].Seat->Name,seatsArray[2].Seat->Phone);
}
20
你是要实现一个顺序存储的线性表吗?你可以写一些函数维护这个线性表,例如像下面这样
你还可以添加对应的get函数,delete函数等等
你还可以添加对应的get函数,delete函数等等
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct ns1__ArrayOfSeat
{
int __sizeSeat; /* sequence of elements <Seat> */
struct ns1__Seat *Seat; /* optional element of type ns1:Seat */
} Seats;
typedef struct ns1__Seat
{
char *Name; /* optional element of type xsd:string */
char *IDCard; /* optional element of type xsd:string */
char *Phone; /* optional element of type xsd:string */
} Seat;
void seatsInit(Seats *p_seats)
{
p_seats->__sizeSeat = 0;
p_seats->Seat = (Seat*)malloc(sizeof(Seat) * 1024);
}
void seatsInsert(Seats *p_seats, Seat *p_seat)
{
(p_seats->Seat)[p_seats->__sizeSeat++] = *p_seat;
}
void printSeats(Seats *p_seats)
{
int i = 0;
for( ; i<p_seats->__sizeSeat; i++)
{
printf("%s %s %s\n", (p_seats->Seat[i]).IDCard, (p_seats->Seat[i]).Name, (p_seats->Seat[i]).Phone);
}
}
void destroySeats(Seats *p_seats)
{
free(p_seats->Seat);
}
int main()
{
Seat s1 = {"测试一","0001","13505710001"};
Seat s2 = {"测试二","0002","13505710002"};
Seat s3 = {"测试三","0003","13505710003"};
Seats ss;
seatsInit(&ss);
seatsInsert(&ss, &s1);
seatsInsert(&ss, &s2);
seatsInsert(&ss, &s3);
printSeats(&ss);
destroySeats(&ss);
getchar();
return 0;
}
10
定义的同时进行初始化
struct {
char *name;
int age;
} stu[2] = { {"jo", 27}, {"y", 30} };
先定义,后初始化,整体赋值
s[1] = (struct stu){23,"hello"}
先定义,后初始化 分开赋值
s[1].age=12;
strcpy(stu[1].name, "hello");
15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ns1__ArrayOfSeat SeatsInfo;
typedef struct ns1__Seat SeatItem;
// 建议用这种方式,或链表的方式。
struct ns1__ArrayOfSeat {
int __sizeSeat;
SeatItem __itemSeats[];
};
struct ns1__Seat {
char *Name;
char *IDCard;
char *Phone;
};
int main(void)
{
SeatsInfo *lpSeats = malloc(sizeof(SeatsInfo) + sizeof(SeatItem) * 3);
if (lpSeats != NULL)
{
// 分配内存
lpSeats->__sizeSeat = 3;
// 假如赋值来自于字符串常量,那么请直接给指针赋值即可。
// 一般来说,这些值都是从其他地方读取,或参数传递等。
// 假如赋值来自于参数或文件,必须用 strdup,最终必须释放。
lpSeats->__itemSeats[0].Name = strdup("测试一");
lpSeats->__itemSeats[0].IDCard = strdup("0001");
lpSeats->__itemSeats[0].Phone = strdup("13505710001");
lpSeats->__itemSeats[1].Name = strdup("测试二");
lpSeats->__itemSeats[1].IDCard = strdup("0002");
lpSeats->__itemSeats[1].Phone = strdup("13505710002");
lpSeats->__itemSeats[2].Name = strdup("测试三");
lpSeats->__itemSeats[2].IDCard = strdup("0003","");
lpSeats->__itemSeats[2].Phone = strdup("13505710003");
// 释放内存
for (int i = 0; i < lpSeats->__sizeSeat; i++)
{
char *lpName = lpSeats->__itemSeats[i].Name;
if (lpName != NULL)
{
free(lpName);
}
char *lpIdCard = lpSeats->__itemSeats[i].IDCard;
if (lpIdCard != NULL)
{
free(lpIdCard);
}
char *lpPhone = lpSeats->__itemSeats[i].Phone;
if (lpPhone != NULL)
{
free(lpPhone);
}
}
free(lpSeats);
}
return 0;
}