小编fle*_*cia的帖子

如何在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;
Run Code Online (Sandbox Code Playgroud)

我正在考虑为字符串本身创建一个结构,以便我可以在结构中使用它们,如下所示:

typedef struct string 
{ 
  char *text;
} *string;
Run Code Online (Sandbox Code Playgroud)

然后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);
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?
谢谢.

c string structure linked-list

15
推荐指数
3
解决办法
9万
查看次数

标签 统计

c ×1

linked-list ×1

string ×1

structure ×1