-2
int main()
{     
    enum a {cat, dog, elephant};
    enum a b;

    b=dog;
    if (b==dog) printf("Y"); else printf("N");

    return 0;
}

The program above works. It shows Y. But I do not see it very useful, so I want to make something similar, but with strings:

int main()
{     
    enum a {cat, dog, elephant};
    enum a b;

    char s[100];
    gets(s);

    b=dog;
    if (b==s) printf("Y"); else printf("N");

    return 0;
}

Even if s=="dog", It doesn't work.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
webpersistence
  • 894
  • 3
  • 12
  • 29

3 Answers3

0

It does not work because you are comparing enum to a pointer.

While char s[100] can indeed hold a string, s simply points to a memory location of the start of your string. In other words, s is a memory chunk, 100 bytes long.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • I know what is the problem. But I do no know how to create a program with those functions. enum a {"cat", "dog", "elephant"}; do not work with "". – webpersistence Dec 30 '14 at 09:22
  • @Abbey: First answer Deduplicator's question: Should the program be C or C++? The solutions will be different. – M Oehm Dec 30 '14 at 09:23
  • @MOehm , I've removed the [tag:c++] tag from the question as OP mentions "It should be in C" in [one of his comments](http://stackoverflow.com/questions/27702564/enum-and-strings#comment43817575_27702564) – Spikatrix Dec 30 '14 at 09:24
0

You could play C preprocessor tricks notably with X-macros (see e.g. this).

You could have a macro invoking some macro P on each animal

#define DO_ANIMALS(P) \
  P(cat) \
  P(dog) \
  P(elephant)

then use it to declare the enum

enum animals_en {
#define DECLARE_ANIMAL(An) En,
DO_ANIMALS(DECLARE_ANIMAL)
};

and later use it to convert a string s to the enum animals_en

enum animals_en convert_string_to_animal(const char*s) {
#define CHECK_STRING_ANIMAL(An) if (!strcmp(s, #An)) return An;
DO_ANIMALS(CHECK_STRING_ANIMAL)
   fprintf(stderr, "bad animal %s\n", s); exit(EXIT_FAILURE);
} 

You may want to look at the preprocessed form obtained with gcc -Wall -C -E yoursource.c > yoursource.i then use some editor or pager on the generated yoursource.i to understand more how that works.

Read documentation of GNU cpp, notably stringification & concatenation.

You could also improve this code to generate something similar to the answer from Vlad from Moscow.

Such tricks would work both in C and in C++, but are more C-like. (In C++11 you would rather try having templates).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Try the following approach

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

int main(void) 
{
    enum Animal { cat, dog, elephant };
    char * animal_name[] = { "cat", "dog", "elephant" };
    enum Animal b;
    size_t n;

    char s[100];
    fgets( s , sizeof( s ), stdin );

    n = strlen( s );
    if ( s[n - 1] == '\n' ) s[n - 1] = '\0';

    b = dog;
    if ( strcmp( animal_name[b], s ) == 0 ) printf( "Y\n" ); 
    else printf( "N\n" );

    return 0;
}

If to enter dog then the output is

Y

Also before comparing strings you could convert the entered string to the lower case that is used in strings of the array of animal names.

Or you could use C# instead of C where enumerators can be converted to strings. :)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335