Reputation: 85
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
Reputation: 108978
#include <stdlib.h>
value
is undefinedz
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
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