I want to initialize a string with every char of it '\0'.(Because I will use gets() later, the first char being not '\0' is OK.)
So I use assign the string with "a".
Here is my code:
typedef struct node
{
char name[12] = "a";
struct node* next;
struct node* prev;
struct node* lead;
} node;
However,
if I change char name[12] = "a";
tochar name[12];
, it will be OK,
if I don't change, it will give an error message:
C:\UsersDesktop\exp1-2\main.c|7|error: expected ':', ',', ';', '}' or '__attribute__' before '=' token|
and C:\Users\Desktop\exp1-2\main.c|25|error: 'node' {aka 'struct node'} has no member named 'name'|
Thank you in advance!
Above shows my part of code, if you wanna see a complete one, HRER are my complete code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct node
{
char name[12] = "a";
struct node* next;
struct node* prev;
struct node* lead;
} node;
node* init()
{
node* huzong = (node*)malloc(sizeof(node));
node* wangzong = (node*)malloc(sizeof(node));
node* zhangzong = (node*)malloc(sizeof(node));
node* lizong = (node*)malloc(sizeof(node));
node* zhangsan = (node*)malloc(sizeof(node));
node* lisi = (node*)malloc(sizeof(node));
node* laoliu = (node*)malloc(sizeof(node));
node* xiaopeng = (node*)malloc(sizeof(node));
node* xiaochen = (node*)malloc(sizeof(node));
strcpy(huzong->name, "胡总");
huzong->lead = NULL;
huzong->prev = NULL;
huzong->next = wangzong;
strcpy(huzong->name, "王总");
wangzong->lead = zhangsan;
wangzong->prev = huzong;
wangzong->next = zhangzong;
strcpy(huzong->name, "张总");
zhangzong->lead = NULL;
zhangzong->prev = wangzong;
zhangzong->next = lizong;
strcpy(huzong->name, "黎总");
lizong->lead = NULL;
lizong->prev = zhangzong;
lizong->next = NULL;
strcpy(huzong->name, "张三");
zhangsan->lead = NULL;
zhangsan->prev = NULL;
zhangsan->next = lisi;
strcpy(huzong->name, "李四");
lisi->lead = xiaopeng;
lisi->prev = zhangsan;
lisi->next = laoliu;
strcpy(huzong->name, "老刘");
laoliu->lead = NULL;
laoliu->prev = lisi;
laoliu->next = NULL;
strcpy(huzong->name, "小彭");
xiaopeng->lead = NULL;
xiaopeng->prev = NULL;
xiaopeng->next = xiaochen;
strcpy(huzong->name, "小陈");
xiaochen->lead = NULL;
xiaochen->prev = xiaopeng;
xiaochen->next = NULL;
return huzong;
}
void list(node* head)
{
printf("%s", head->name);
list(head->lead);
list(head->next);
}
node* find(node* head, char* ex_name)
{
int i = 1;
i = strcmp((head->name), *ex_name);
if(i==0)
{
return head;
}
find(head->lead, ex_name);
find(head->next, ex_name);
return NULL;
}
void lead(node* new_node, node* existing)
{
existing->lead = new_node;
new_node->lead = NULL;
new_node->next = NULL;
new_node->prev = NULL;
}
void follow(node* new_node, node* existing)
{
existing->next = new_node;
new_node->prev = NULL;
new_node->lead = NULL;
new_node->next = NULL;
}
int main()
{
char input[60]="a";
char new_name[12]="a";
char ex_name[12]="a";
while(1)
{
node* head;
node* temp = NULL;
head = init();
gets(input);
for(int i = 0; i < 50; i++)
{
//init初始化实现
if(input[i]=='i'&&input[i+1]=='n'&&input[i+2]=='i'
&&input[i+3]=='t')
{
init();
printf("链表构造完成,现有9名员工");
}
//list遍历功能实现
if(input[i]=='l'&&input[i+1]=='i'&&input[i+2]=='s'
&&input[i+3]=='t')
{
list(head);
}
//lead功能实现
if(input[i]=='l'&&input[i+1]=='e'&&input[i+2]=='a'
&&input[i+3]=='d')
{
for(int j=0; j<=i-2; j++)
{
new_name[j] = input[j];
}
printf("%s", new_name);
for(int k=i+8; k<=i+20; k++)
{
ex_name[k-i-8] = input[k];
}
printf("%s", ex_name);
node* new_node = (node*)malloc(sizeof(node));
strcpy(new_node->name, new_name);
printf("strcpy完成");
temp = find(head, ex_name); //问题在此
printf("find完成");
temp->lead = new_node;
}
}
}
}