user-0 提问: c语言结构休数据赋值

回复:
user: user-1
content: #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; }

user: user-2
content: “变量赋值”和“变量初始化”不是一回事！ “变量赋值”发生在运行期，其写法遵循赋值语法规定。 “变量初始化”发生在编译期或运行期，其写法遵循初始化列表语法规定。  凡是问我为什么贴下面这段话的人都不适合当程序员： 关于自己是否适合编程的很简单的测试： 在报纸或杂志上随便找一段约1000字的文章，在Word中输入一遍。输完后再参考下面答案：  A里面有10处以上文字或标点错误 B里面没有文字或标点错误并敢为此跟人打赌 C里面没有文字或标点错误并且字体和排版完全与原稿一致 D打印在半透明的纸上和原稿重叠在一起检查一模一样，且自我感觉很有成就感  A不适合编程（理由：打字准确度偏低、粗心大意） B初级程序员（理由：打字准确度很高、认真细致、自信、理解全角半角概念） C高级程序员（理由：在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证） D软件项目经理（理由：能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业！）  如果想从A变成B的话，到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”

user: user-3
content: 字符串赋值使用strcpy

user: user-4
content: 定义的同时进行初始化 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");

user: user-5
content: 你是要实现一个顺序存储的线性表吗？你可以写一些函数维护这个线性表，比如像下面这样 你还可以添加对应的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; }

user: user-6
content: struct ns1__Seat ss [] ={ {"测试一","0001","13505710001"},  {"测试二","0002","13505710002"},  {"测试三","0003',"13505710003"}  }; struct ns1__ArrayOfSeat seats = { _countof(ss), ss };

user: user-7
content: 简单写了下，可以这样赋值。  #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); }