0

I have created a .h file in C that contains a enum 'app'. This enum value need to be accessed in the main.c program. My code looks something like this:

 state.h file

#ifndef State_H_
#define State_H_

#define enum app {A,D,H};

#endif

My main program is :

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

char letters[] ={'AAAA'};
if( letters[0] ==State.app[0])
{
   printf("the first letter is matching");
}

But here in the if statement the State.app[0] gives an error

Error: State could not be resolved, app could not be resolved.

Could somebody please suggest a way in which I could access the enum values from the header file. Please note that the int main and return are part of the program. I have just not included them here. Thank you for your help in advance.

9
  • do (don't) you get any warnings? Commented Jul 31, 2015 at 14:02
  • @SouravGhosh: Nope. I just get this error. No warnings. Commented Jul 31, 2015 at 14:04
  • 1
    Do not use single character names for global objects! Commented Jul 31, 2015 at 14:04
  • look closely 'AAAA'.... Commented Jul 31, 2015 at 14:06
  • 1
    And.. #define enum .... is a really weird construct. Well, what @Olaf said. Commented Jul 31, 2015 at 14:11

2 Answers 2

2

State doesn't define a 'namespace' (like in C#), so you can't access the type (with enum you're defining a type) by using as prefix the name of the header.

you should define enum like this:

enum app = { AA, BB, CC };

You can define 'State' as a local variable if you want.

enum app State;

Then assign a value:

State = AA;

What you're trying to do it's still unclear to me, though.

Sign up to request clarification or add additional context in comments.

Comments

1

Many, many issues.

First of all,

#define enum app {A,D,H};

doesn't create an enumerated type; instead, it creates a macro named enum that will be replaced with the text app {A,D,H}; whenever it appears in your code. If you want to create an enumerated type, just write

enum app {A, D, H};

Second of all, an enumerated type does not act like an array; you don't access enumeration values using array subscript syntax such as app[0]. You use the enumeration constant A, D, or H. Also, the header file does not create a namespace; you don't use State as a prefix for anything.

Finally, the enumeration constant A is not the same thing as the character constant 'A', and it won't automatically have the same value. You will have to explicitly initialize the enumeration constant with the value you want, like so:

enum app { A = 'A', D = 'D', H = 'H' };

So, putting everything together, your code will look something like this:

/**
 * State.h
 */
#ifndef State_H_
#define State_H_

enum app { A = 'A', D = 'D', H = 'H' };

#endif

/**
 * main.c
 */
#include <stdlib.h>
#include <stdio.h>
#include "State.h"

int main( void )
{
  char letters[] ={'A', 'A', 'A', 'A'}; // thanks NiBZ.
  if( letters[0] == A)      // no qualifier, just the bare enum constant
  {
     printf("the first letter is matching");
  }
  return 0;
}

EDIT

As NiBZ points out, there's a problem with the declaration

char letters[] ={'AAAA'}; 

If you want a simple array of characters, then the initializer needs to look like

char letters[] = {'A', 'A', 'A', 'A' };

If you want letters to be a string (sequence of characters terminated by a 0-valued byte), then the initializer needs to look like either

char letters[] = {'A', 'A', 'A', 'A', 0 };

or

char letters[] = "AAAA";

END EDIT

Having said all that, this code feels very confused. If the enumeration constant A has special meaning beyond simply being the value of the character constant 'A', you should use a more meaningful name for your enumeration constant. For example, if 'A' indicates the beginning of a record or something use a name like RECORD_BEGIN instead of A:

if ( letters[0] == RECORD_BEGIN )

Otherwise, just use the bare character constant:

if ( letters[0] == 'A' )

2 Comments

You kept the char letters[] = {'AAAA'};. Using something like char letters[] = "AAAA"; would be ... better ;)
@NiBZ: derp. I'll fix that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.