在C文件中访问struct

在C文件中访问struct,c,struct,C,Struct,刚开始使用C,处理头文件声明等。我正试图编写一个测试程序,从结构中获取温度的名称和度值,例如37开尔文,然后将其转换为所需的温度。 我在头文件中编写了一些typedef、struct和enum声明,但现在我很难从主文件访问它们。我得到的错误是“错误:请求非结构或联合中的成员‘lampotila’” 我的头文件如下所示: #ifndef asteet_h #define asteet_h typedef float Lampotila; typedef char Asteikko[20]; st

刚开始使用C,处理头文件声明等。我正试图编写一个测试程序,从结构中获取温度的名称和度值,例如37开尔文,然后将其转换为所需的温度。 我在头文件中编写了一些typedef、struct和enum声明,但现在我很难从主文件访问它们。我得到的错误是“错误:请求非结构或联合中的成员‘lampotila’”

我的头文件如下所示:

#ifndef asteet_h
#define asteet_h

typedef float Lampotila;
typedef char Asteikko[20];
struct Lampotila {
    Lampotila lampotila;
    Asteikko asteikko;

};



enum Asteikko{
Celsius = 1,
Fahrenheit = 1,
Kelvin = 1
};
float muunna(Lampotila,Asteikko);
#endif
我的主要操作如下所示:

#include <stdio.h>
#include <string.h>
#include "asteet.h"

int main(int argc,char *argv[]){

float muunna(Lampotila a, Asteikko b){
    if(a.asteikko == "Celsius" && b == "Fahrenheit"){
        return(a.lampotila*1.8+32);
    }
    else if(a.asteikkko == "Fahrenheit" && b == "Celsius"){
        return((a.lampotila-32)/1-8);
    }
    else if(a.asteikko == "Celsius" && b == "Kelvin"){
        return(a.lampotila + 273.15);
    }
    else if(a.asteikkko == "Kelvin" && b == "Celsius"){
        return(a.lampotila - 273.15);
    }
    return 0;
}
return 0;
}

纠正以下问题

  • 使用结构时,请指定struct关键字,或者键入定义结构的
    struct
    因此,行“
    float muunna(Lampotila a,asteiko b){
    ”将是“
    float muunna(struct Lampotila a,asteiko b){
    ”。这将清除错误

  • else if(a.asteikko==“华氏”和&b==“摄氏”){
    是错误的,因为
    asteikko
    在您的结构中只定义了2'
    k

  • 您可以在main之外定义函数,并通过传递参数来调用函数

  • 你的函数在main中声明,你的float和struct的typedef名称相同。你也不能将字符串与
    =
    进行比较,你需要
    strcmp
    。帮你自己一个忙,买本书吧。这段代码中有很多错误,在几章之后你就可以解决了。这些代码中没有一个是正确的毫无意义。编程时,你不能“猜测”。你实际上必须理解你写的每一行都做了什么。
    Lampotila a = {23.5, Celsius};
    Lampotila b = {79.7, Fahrenheit};
    Lampotila c = {285.8, Kelvin};
    Asteikko kelvin = Kelvin;
    printf("23.5 C on %.2f K\n", muunna(a, kelvin));
    printf("79.7 F on %.2f C\n", muunna(b, Celsius));