[英]Error with struct linked list in Keil
I was working on a project of mine and the code I have posted below ran successfully in Dev C complete with O/P. 我正在开发一个我的项目,下面发布的代码在带有O / P的Dev C中成功运行。 However Keil is spitting out errors which I have unfortunately not been able to pin down. 但是,Keil吐出了一些错误,但不幸的是,我无法确定这些错误。
Some samples: 一些样本:
8051TEST.C(322): warning C198: sizeof returns zero 8051TEST.C(322):警告C198:sizeof返回零
8051TEST.C(324): error C230: 'switc': unknown struct/union/enum tag 8051TEST.C(324):错误C230:'switc':未知的struct / union / enum标签
8051TEST.C(324): error C204: 's0': undefined member 8051TEST.C(324):错误C204:'s0':未定义成员
8051TEST.C(325): error C230: 'switc': unknown struct/union/enum tag 8051TEST.C(325):错误C230:'switc':未知的struct / union / enum标签
8051TEST.C(325): error C204: 's1': undefined member 8051TEST.C(325):错误C204:'s1':未定义成员
8051TEST.C(326): error C230: 'switc': unknown struct/union/enum tag 8051TEST.C(326):错误C230:'switc':未知的struct / union / enum标签
Would really like a fresh pair of eyes. 真的会像一双新鲜的眼睛。
Thanks, Aurelius 谢谢,奥雷留斯
typedef struct
{
int s0;
int s1;
int s2;
int s3;
int c;
int name_len;
int struct_size;
char sw_name[10]; //variable length array must be last.
}switc;
//Memory allocation and initialisation of structure
struct switc *createsw(struct switc *s, int id1, int id2, int id3, int id4,
int id5, char a[])
{
s = malloc( sizeof(*s) + sizeof(char) * strlen(a) );
s->s0 = id1;
s->s1 = id2;
s->s2 = id3;
s->s3 = id4;
s->c = id5;
s->name_len = strlen(a);
strcpy(s->sw_name, a);
s->struct_size = ( sizeof(*s) + sizeof(char) * strlen(s->sw_name) );
return s;
}
struct newcard //create your own configuration
{
int pos_sw1;
int pos_sw2;
int pos_sw3;
int pos_sw4;
int pos_sw5;
int pos_sw6;
int pos_sw7;
};
struct newcard *createcard(struct newcard *c, int sr1, int sr2, int sr3,
int sr4, int sr5, int sr6, int sr7)
{
c = malloc( sizeof(*c));
c->pos_sw1 = sr1;
c->pos_sw2 = sr2;
c->pos_sw3 = sr3;
c->pos_sw4 = sr4;
c->pos_sw5 = sr5;
c->pos_sw6 = sr6;
c->pos_sw7 = sr7;
return c;
}
My int main() as requested. 我的int main()根据要求。 Bit of a mish mash so apologise in advance. 有点糊状,所以提前道歉。 The whole thing worked flawlessly in Dev C. 整个开发环境在Dev C中完美无瑕。
int main()
{
Lcd4_Init();
Lcd4_Clear();
switc *s1, *s2, *s3, *s4, *s5, *s6 , *s7;
int id1[5],id2[5],id3[5],id4[5],id5[5],id6[5],id7[5],pos=3;
int cd1[7],cd2[7],cd3[7],cd4[7],cd5[7],cd6[7],sw1=0,sw2=0,sw3=0,sw4=0,sw5=0,
sw6=0,s
w7=0,card=0,menu=0,n=7;
int *cd7=(int *)malloc(n * sizeof(int));
M:menu=main_menu();
if(menu==1)//menu to monitor functions
{
confsel();
goto M;
}
//assign number to card based on option selected.
if(menu==2){
card=cardsel();
}
if(menu==3)//Mini info guide
{
printf("\nPCB for 6x8 through(SW 1,2,3,4,5,7) + 1x11 through(SW 6) ROTARY SWITCH(s).\n");
printf("\nSwitch LAYER(s) by 1:8 and 1:16 DEMUX.\n");
printf("\nTRIGGER to DEMUX(s) by 8255A-5 PPI.\n");
printf("\nAT89C51RD2 powering device.\n");
printf("\nEnter card when PROMPTED.\n");
printf("\nCOOLING FAN provided.\n");
printf("\nPOTENTIOMETER P(1-5) must be ANTI-CLOCKWISE.\n");
goto M;
}
switch(card)
{
case 1:
printf("Power Card assigned\n");
memcpy(cd1,&pcard,sizeof(cd1));
sw1=cd1[0];
sw2=cd1[1];
sw3=cd1[2];
sw4=cd1[3];
sw5=cd1[4];
sw6=cd1[5];
sw7=cd1[6];
break;
case 2:
printf("Firing Card assigned\n");
memcpy(cd2,&fcard,sizeof(cd2));
sw1=cd2[0];
sw2=cd2[1];
sw3=cd2[2];
sw4=cd2[3];
sw5=cd2[4];
sw6=cd2[5];
sw7=cd2[6];
break;
case 3:
printf("Const. Curr. Card assigned\n");
memcpy(cd3,&c1card,sizeof(cd3));
sw1=cd3[0];
sw2=cd3[1];
sw3=cd3[2];
sw4=cd3[3];
sw5=cd3[4];
sw6=cd3[5];
sw7=cd3[6];
break;
case 4:
printf("Const. Curr. 2 Card assigned\n");
memcpy(cd4,&c2card,sizeof(cd4));
sw1=cd4[0];
sw2=cd4[1];
sw3=cd4[2];
sw4=cd4[3];
sw5=cd4[4];
sw6=cd4[5];
sw7=cd4[6];
break;
case 5:
printf("Amplifier Card assigned\n");
memcpy(cd5,&acard,sizeof(cd5));
sw1=cd5[0];
sw2=cd5[1];
sw3=cd5[2];
sw4=cd5[3];
sw5=cd5[4];
sw6=cd5[5];
sw7=cd5[6];
break;
case 6:
printf("NC (ATM) Card assigned\n");
memcpy(cd6,&nccard,sizeof(cd6));
sw1=cd6[0];
sw2=cd6[1];
sw3=cd6[2];
sw4=cd6[3];
sw5=cd6[4];
sw6=cd6[5];
sw7=cd6[6];
break;
case 7:
printf("Custom Card assigned\n");
incdec(cd7,n);//function to extract values to cd7[]
struct newcard *c1;
c1=createcard(c1,*(cd7 + 0),*(cd7 + 1),*(cd7 + 2),*(cd7 + 3),*(cd7 + 4),*(cd7 + 5),*(cd7 + 6));
sw1=c1->pos_sw1;
sw2=c1->pos_sw2;
sw3=c1->pos_sw3;
sw4=c1->pos_sw4;
sw5=c1->pos_sw5;
sw6=c1->pos_sw6;
sw7=c1->pos_sw7;
break;
default:
printf("Invalid Card assigned\n");
break;
}
Menu subroutines on demand. 菜单子例程按需提供。
int main_menu()
{
const char Main_Menu [3][11] =
{
"1.CONFIG",//TBD
"2.LOAD",//load PRELOADED_CARD or create CUSTOM_CARD
"3.INFO"//check temp/voltage/credits
};
int menu=0,j=0,i=0;
Lcd4_Set_Cursor(1,3);
M:Lcd4_Write_String("Select your option:");
Lcd4_Set_Cursor(2,4);
Lcd4_Write_String(Main_Menu[j]);
do{
if(RK==0&&j<3)//Right Key
{
j++;
if(j>=0||j<3)
{
Lcd4_Set_Cursor(2,4);
Lcd4_Write_String(Main_Menu[j]);
}
}
if(LK==0&&j>0)//Left Key
{
j--;
if(j>=0||j<3)
{
Lcd4_Set_Cursor(2,4);
Lcd4_Write_String(Main_Menu[j]);
}
}
if(UK==0)//Up Key
{
goto M;
}
if(EK==0)//Enter Key
{
if(j>=0||j<3)
{
menu=j+1;
i=1;
}
}
return menu;
}while(i!=1);
}
Error messages on demand (once int main starts it all goes downhill.) 按需提供错误消息(一旦int main启动,一切都会下坡。)
8051TEST.C(464): error C141: syntax error near 'switc' 8051TEST.C(464):错误C141:'switc'附近的语法错误
8051TEST.C(464): error C202: 's1': undefined identifier 8051TEST.C(464):错误C202:'s1':未定义的标识符
8051TEST.C(465): error C141: syntax error near 'int' 8051TEST.C(465):错误C141:'int'附近的语法错误
8051TEST.C(465): error C202: 'id1': undefined identifier 8051TEST.C(465):错误C202:'id1':未定义的标识符
8051TEST.C(466): error C141: syntax error near 'int' 8051TEST.C(466):错误C141:'int'附近的语法错误
8051TEST.C(466): error C202: 'cd1': undefined identifier 8051TEST.C(466):错误C202:'cd1':未定义的标识符
8051TEST.C(467): error C141: syntax error near 'int' 8051TEST.C(467):错误C141:'int'附近的语法错误
8051TEST.C(467): error C202: 'cd7': undefined identifier 8051TEST.C(467):错误C202:'cd7':未定义的标识符
8051TEST.C(469): error C202: 'menu': undefined identifier 8051TEST.C(469):错误C202:'菜单':未定义的标识符
8051TEST.C(471): error C202: 'menu': undefined identifier 8051TEST.C(471):错误C202:'菜单':未定义的标识符
8051TEST.C(478): error C202: 'menu': undefined identifier 8051TEST.C(478):错误C202:'菜单':未定义的标识符
and 200 more of these C202s. 以及另外200架C202。
当您对一个结构体进行typedef
命名时,您只需要使用名称switc
所以请不要对其进行typedef命名,或者不要对变量/参数使用struct switc
。
Basically cleblanc's answer mentions it, but I want to make it a bit more clear. cleblanc的回答基本上提到了它,但我想使其更加清晰。
The definition of the struct and the typedef is not used correctly: struct和typedef的定义使用不正确:
What you use: 使用方法:
typedef struct /* IMPORTANT: NO TAG HERE! */
{
int s0;
int s1;
...
} switc;
Defined types: switc
定义的类型: switc
Valid usage: 有效用法:
switc *s1, *s2, *s3, *s4, *s5, *s6 , *s7;
s1->s1 = ...;
Invalid usage: 无效的用法:
struct switc *s;
s->s1 = ...;
What you think you are using: 您认为您正在使用什么:
typedef struct switc /* NOTE: STRUCT WITH TAG. */
{
int s0;
int s1;
...
} switc;
Defined types: struct switc
and switc
定义的类型: struct switc
和switc
Valid usage: 有效用法:
switc *s1, *s2, *s3, *s4, *s5, *s6 , *s7;
s1->s1 = ...;
struct switc *s;
s->s1 = ...;
EDIT: 编辑:
For your main function the situation is different. 对于您的主要功能,情况有所不同。 Here the compiler does not complain about unknown type switch
. 在这里,编译器不会抱怨未知类型switch
。 Instead the compiler is unhappy with your function body. 相反,编译器对您的函数体不满意。
int main()
{
Lcd4_Init();
Lcd4_Clear();
switc *s1, *s2, *s3, *s4, *s5, *s6 , *s7;
Prior to C99 standard, it was mandatory to put all variable definitions at the top of the function body. 在C99标准之前,必须将所有变量定义放在功能主体的顶部。 Is it was mentioned in some comments that Keil Dev C is rather old style, I would expect that it only supports K&R and C89. 是否有人在评论中提到Keil Dev C是相当老的样式,我希望它仅支持K&R和C89。 (Last time I used a Keil C compiler for C16x was in 2006. At that time no C99 support was available.) (上一次我在2006年为K16x使用Keil C编译器。当时没有C99支持。)
You are violating this rule by calling some LCD4_...
functions first. 您首先调用某些LCD4_...
函数违反了此规则。 As no variable definitions are allowed afterwards, no type name is allowed as well. 由于此后不允许使用变量定义,因此也不允许使用类型名称。
To fix it, simply change your order of lines: 要解决此问题,只需更改行顺序:
int main()
{
switc *s1, *s2, *s3, *s4, *s5, *s6 , *s7;
Lcd4_Init();
Lcd4_Clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.