我在制作基于C, 中单链表的数据库时遇到了困难,这不是因为链表概念,而是因为结构本身中的字符串字段。如何使用C结构中的字符串字段?
这是C中的一个赋值,据我所知(我是一个新手),C不会将'string'识别为数据类型。
这是我的结构代码如下所示:
typedef struct
{
int number;
string name;
string address;
string birthdate;
char gender;
} patient;
typedef struct llist
{
patient num;
struct llist *next;
} list;
我想制作一个结构的字符串的自己,让我可以在结构中使用它们,就像这样:
typedef struct string
{
char *text;
} *string;
然后,我会malloc()
每当他们需要创建字符串类型(char数组)的新数据。
typedef struct string
{
char *text;
} *string;
int main()
{
int length = 50;
string s = (string) malloc(sizeof string);
s->text = (char *) malloc(len * sizeof char);
strcpy(s->text, patient.name->text);
}
有人可以帮我弄清楚这一点吗?
谢谢。
你[不应该使用类型](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858),而使用malloc。 – 2012-04-15 12:56:35
编号'patient'没有全局声明,这是一个typdef。但即使它是全球性的,你也不会像这样初始化它。 – kralyk 2012-04-15 13:04:17
@ kralyk - 正确的,我自己意识到并纠正了。 – Attila 2012-04-15 13:05:14