简体   繁体   English

如何检索枚举类型变量中的元素?

[英]How to retrieve elements in an enum type variable?

When the code is compiled, it doesn't show the values inserted to the enum variable in structure 编译代码时,它不会在结构中显示插入到enum变量中的值

This code only shows the keyboard input 0 or 1 此代码仅显示键盘输入0或1

#include<stdio.h>
#define Max_CHARS_NAME 100

struct gps_point{
    double latitude;
    enum latitude_pole {North,South} pole;
    double longitude;
    enum longitude {East,West} dire;
    char location_name[Max_CHARS_NAME];
}g;

int main(){
    char arr[Max_CHARS_NAME];
    int i = 0;

    printf("Enter location\n");
    scanf("%s", &g.location_name);  
    strcpy(arr,g.location_name);

    printf("Enter latitude\n");
    scanf("%lf", &g.latitude);

    printf("Enter latitude pole North - 0, South - 1\n");
    scanf("%d",&g.pole);

    //g.latitude_pole = g.pole;

    printf("Enter longitude dire East - 0, West - 1\n");
    scanf("%d", &g.dire);

    printf("Enter longitude\n");
    scanf("%s", &g.longitude);

    printf("%s is situated at (Latitude : %s %lf , Longitude: %s %lf). \n",arr,g.pole,g.latitude,g.dire,g.longitude);

    return 0;
}

See the below example, 请参阅以下示例,

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; 

int main() 
{ 
    enum week day; 
    day = Wed; 
    printf("%d",day); 
    return 0; 
}  

Here we declare a variable day of type week and assign one(numerical) value in the list to the variable using a "string" but it isn't possible to do the other way. 在这里,我们声明一个类型为week的变量day,并使用“字符串”将列表中的一个(数字)值分配给该变量,但无法执行其他方法。

This is equivalent to 这相当于

enum latitude_pole {
  North = 0,
  South = 1
} pole;

This defines the variable pole whose possible values are North(0) and South(1). 这定义了可变pole其可能值为North(0)和South(1)。

It is equivalent to writing 相当于写作

enum latitude_pole {
  North = 0,
  South
} pole;

because in case you initialize one member the next one will be +1 (previous member). 因为如果您初始化一个成员,那么下一个成员将是+1 (先前的成员)。

In your code the missing of initialization will have it insert by default the value 0 for the first member and 1 for the second member. 在您的代码中,缺少初始化将使其默认情况下为第一个成员插入值0,为第二个成员插入值1。

If you don't give the enumeration constants any values, the compiler will do it silently. 如果您不给枚举常量任何值,则编译器将以静默方式进行操作。

The first one will always be given value 0 , and every following enumeration constant will get the value of previous constant + 1. 第一个始终将被赋予值0 ,每个随后的枚举常量将获得先前常量+ 1的值。

In your case {North,South} , North is guaranteed to be 0 and South guaranteed to be 1. The type of these enumeration constants is guaranteed to be 100% compatible with int . 在您的情况下{North,South} ,保证North为0,并且South保证为1。这些枚举常量的类型保证与int 100%兼容。

Had you typed only some values explicitly, the rule where every constant getting the value of the previous + 1 is still applicable. 如果您只明确键入了一些值,则该规则适用于每个常量获取前一个+ 1的值的规则。 So if you would do {North,South,East=5,West} , they would get the values 0, 1, 5, 6 . 因此,如果您执行{North,South,East=5,West} ,则它们将获得值0, 1, 5, 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM