C语言变量定义 splint

发布于 2021-11-16 08:33:44 字数 3073 浏览 753 评论 1

按照 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2021-11-17 05:02:53

存放指针的数组。。。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文