asir
asir

Reputation: 85

comparing enum value with enum instance

Why can't it be done like this?

    #include<stdio.h>
    #include<stdlib.h>
    struct test
    {
        value z;
        int x;
    };

    main()
    {
        enum value {a,b,c,d};
        struct test come;
        come.z = 2;
        if (a == z) printf("they match ");
        else printf("dont match");
    }

Upvotes: 0

Views: 3463

Answers (2)

pmg
pmg

Reputation: 108978

  • You don't need to #include <stdlib.h>
  • value is undefined
  • z by itself does not exist. It exists only as a member of struct test

This works (I've added whitespace, removed stdlib, changed value to int, added return type and parameters info to main, changed the z inside the if, added '\n' to the prints, and added return 0;):

#include <stdio.h>

struct test
{
    int z;
    int x;
};

int main(void)
{
    enum value {a, b, c, d};
    struct test come;
    come.z = 2;
    if (a == come.z) printf("they match\n");
    else printf("don't match\n");
    return 0;
}

The definition of enum value really should be outside any function.

Upvotes: 0

Lundin
Lundin

Reputation: 213711

Make the enum a typedef enum and place it above the struct.

#include <stdio.h>
#include <stdlib.h>

typedef enum { a,b,c,d } value;

struct test
{
  value z;
  int x;
};

int main()
{
  struct test come;
  come.z = 2;

  if (a == come.z)
    printf("they match ");
  else
    printf("dont match");
}

Edit: fixed some minor typos.

Upvotes: 2

Related Questions