error: initializer element is not constant 错误

djinglan 2012-04-09 04:23:59
typedef struct student {
char name[20]
char phone[20];
bool flag;
struct student *next;
}stu;

stu *Read(stu * phead);
struct student *head = NULL;

head = Read(head);


phonetext.c:22: warning: data definition has no type or storage class
phonetext.c:22: error: conflicting types for ‘head’
phonetext.c:20: note: previous definition of ‘head’ was here
phonetext.c:22: warning: passing argument 1 of ‘Read’ makes pointer from integer without a cast
phonetext.c:14: note: expected ‘struct stu *’ but argument is of type ‘int’
phonetext.c:22: warning: initialization makes integer from pointer without a cast
phonetext.c:22: error: initializer element is not constant
phonetext.c: In function ‘Schedle’:


不知道怎么解决,谢谢帮忙。
...全文
1239 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
djinglan 2012-04-09
  • 打赏
  • 举报
回复
问题解决了。
原因在于 struct student *head = NULL;
head = Read(head);

在main()函数外,这种写法在gcc 编译其中不能使用,但是在g++ 编译其中支持这种写法。

改正该错误的方法是 在mian中 调用 head = Read(head);即可成功。


非常感谢大家的帮助,非常感谢。
Wenxy1 2012-04-09
  • 打赏
  • 举报
回复
用c99标准,试试加-std=c99
djinglan 2012-04-09
  • 打赏
  • 举报
回复
非常抱歉,粘贴的不好。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

typedef struct student
{
char name[20];
char phone[20];
bool flag;
struct student *next;
}stu;

struct student * Read(struct student *phead);
stu *Add(stu *phead);
void Write(stu *phead);
void Search(stu *phead);
void Delete(stu *phead);
void Modify(stu *phead);
struct student *head = Read(head);

void Schedle()
{
while(true)
{
printf("\n输入操作命令:>");
char command[20];
scanf("%d",&command);
if (strcmp(command,"add") == 0)
{
head = Add(head);
}
else if (strcmp(command,"update") == 0)
{
Modify(head);
}
else if (strcmp(command,"delete") == 0)
{
Delete(head);
}
else if (strcmp(command,"find") == 0)
{
Search(head);
}
else if(strcmp(command,"exit") == 0)
{
Write(head);
exit(0);
}
else
{
printf("对不起,您输入了不能识别的命令\n");
}
}
djinglan 2012-04-09
  • 打赏
  • 举报
回复
else
51 {
52 printf("对不起,您输入了不能识别的命令\n");
53 }
54 }
djinglan 2012-04-09
  • 打赏
  • 举报
回复
124 {
125 printf("姓名:%s |电话号码:%s 记录将要清除,请确认(yes/no):", ps->nam
126 char command[20];
127 scanf("%s",command);
128 if (strcmp(command, "yes") == 0)
129 {
130 ps->flag = true;
131 printf("记录清除完毕\n");
132 }
133 else
134 {
135 printf("查无记录\n");
136 }
137 }
138 }
139
140
141 void Modify(stu *phead)
142 {
143 stu *ps = Find(phead);
144 if (ps != NULL)
145 {
146 printf("姓名:%s |电话号码: %s\n",ps->name,ps->phone);
147 char name[20];
148 char phone[20];
149 printf("请输入要修改后的名字:");
150 scanf("%s",name);
151 printf("请输入要修改后的电话号码:");
152 scanf("%s",phone);
153 strcpy(ps->phone,phone);
154 strcpy(ps->name,name);
155 printf("记录修改望比\n");
156 }
157 else
158 {
159 printf("查无记录\n");
160 }
161 }
162
163 stu * Read(stu *phead)
164 {
165 FILE *fp = fopen("data.dat", "rb");
166 if (fp == NULL)
158,5 48%
wwyyxx26 2012-04-09
  • 打赏
  • 举报
回复
你这代码也没有几行,要不都打出来看看?
昵称很不好取 2012-04-09
  • 打赏
  • 举报
回复
head重定义了,看看phonetext.c的第20行,是不是也定义了head
djinglan 2012-04-09
  • 打赏
  • 举报
回复
已经定义了head ,如下

stu *Read(stu * phead);
struct student *head = NULL;

head = Read(head);

实在是找不到有什么问题,还有就是,c99标准,只支持全局变量和static变量,定义时初始化,但是,我试了另外的定义方式,struct student *head = Read(head);
这样也是出现上述问题,请指点。谢谢
Wenxy1 2012-04-09
  • 打赏
  • 举报
回复
phonetext.c:22: error: conflicting types for ‘head’

检查phonetext.c文件的22行, 冲突的head类型. 是否包含了相应的头文件,或者是否定义了head 类型。

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧