如何修复结构返回函数中的'期望标识符'或'''int''错误

问题描述 投票:-4回答:1

我正在创建C代码,从单个函数输出直径,面积,周长。我正在使用一个结构来输出数据。但是,在创建输出结构的函数时,我收到以下错误:'expected identifier or'('before'int'''

我已经尝试过明确的代码说,但无济于事。

#include <stdio.h>

struct circle
{
    int diameter;
    int area;
    int circumference;
};
    typedef struct circle one;

struct properties (int r)
{
    struct circle.one.diameter = 2 * r;
    struct circle.one.area = (22 * r * r) / 7;
    struct circle.one.circumference = (2 * 22 * r) / 7;

    return (one);
}

int main ()
{
    int a;
    int result;
    printf ("text");
    scanf ("%d", &a);
    result = properties (a);
    printf ("%d%d%d", result );

    return 0;
}

我希望输出是直径,面积,周长的值。

c
1个回答
2
投票

这是修正了许多错误的工作程序。请注意,它正在进行整数除法,因此结果向下舍入到最接近的整数。

#include <stdio.h>

struct circle
{
    int diameter;
    int area;
    int circumference;
};

typedef struct circle one;

one properties (int r)                          // use the typedef
{
    one calcs;                                  // define a struct
    calcs.diameter = 2 * r;                     // clean up the act
    calcs.area = (22 * r * r) / 7;
    calcs.circumference = (2 * 22 * r) / 7;

    return calcs;
}

int main (void)                                 // full definition
{
    int a;
    one result;                                 // this should be a struct
    printf ("radius: ");                        // sensible prompt
    scanf ("%d", &a);
    result = properties (a);
    // space separate the output, pass each value
    printf ("%d %d %d", result.diameter, result.area, result.circumference);
    return 0;
}

程序输出

radius: 3
6 28 18
© www.soinside.com 2019 - 2024. All rights reserved.