C语言变量定义 splint
按照 [C语言入门经典(第四版)][(美)霍顿]
书里写练习了一段代码.用splint检查了一下.出下面的问.说是未定义的变量错误.
不明白怎么写才算了定义了变量.如果使用/*@out@*/只是忽略检查.
下面是检查结果
==============================================
Splint 3.1.2 --- 03 May 2009
struct.c: (in function main)
struct.c:57:12: Passed storage phorse[]->name not completely defined
(*(phorse[]->name) is undefined): printf (..., phorse[i]->name, ...)
Storage derivable from a parameter, return value or global is not defined.
Use /*@out@*/ to denote passed or returned storage which need not be defined.
(Use -compdef to inhibit warning)
struct.c:57:12: Value *(phorse[]->name) used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (Use -usedef to inhibit warning)
struct.c:57:29: Field phorse[]->age used before definition
struct.c:57:45: Field phorse[]->height used before definition
struct.c:61:10: Unqualified storage phorse[i] passed as only param:
free (phorse[i])
Unqualified storage is transferred in an inconsistent way. (Use
-unqualifiedtrans to inhibit warning)
Finished checking --- 5 code warnings
下面是代码
==============================
#include <stdio.h> #include <ctype.h> #include <stdlib.h> int main (void) { struct horse { int age; int height; char name[20]; char father[20]; char mother[20]; }; struct horse *phorse[50]; int hcount = 0; char test = ''; int i = 0; for (hcount = 0; hcount < 50; hcount++) { printf("nDo you want to enter details of a%s horse (Y or N)?", hcount>0?"nother " : "" ); (void)scanf(" %c", &test); if ((char)tolower(test) == 'n') break; phorse[hcount] = (struct horse *)malloc(sizeof(struct horse)); if (phorse[hcount] == NULL) { exit(EXIT_FAILURE); } printf("nEnter the name of the horse: "); (void)scanf("%s", phorse[hcount]->name); printf("nHow old is %s? ", phorse[hcount]->name); (void)scanf("%d", &phorse[hcount]->age); printf("nHow high is %s (in hands)? ", phorse[hcount]->name); (void)scanf("%d", &phorse[hcount]->height); printf("nWho is %s's father? ", phorse[hcount]->name); (void)scanf("%s", phorse[hcount]->father); printf("nWho is %s's mother? ", phorse[hcount]->name); (void)scanf("%s", phorse[hcount]->mother); } for (i = 0; i < hcount; i++) { printf("nn%s is %d years old, %d hands high, ", phorse[i]->name, phorse[i]->age, phorse[i]->height); printf(" and has %s and %s as parents.n", phorse[i]->father, phorse[i]->mother); free(phorse[i]); phorse[i] = NULL; } return 0; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存放指针的数组。。。