Srv19
Srv19

Reputation: 3618

How to declare function with anonymous enum in function signature?

One can have a function that contains an anonymous enum in its signature. However, when i tried moving function signature to the header file, i have encountered a compiler errors.

void myfunct (enum {a=1} param); 
void myfunct (enum {a=1} param)
{}

That yields the following:

error: conflicting types for ‘myfunct’

If enum is named (and moved outside of the function declaration), there is no error.

What would be a correct syntax to use?

Upvotes: 2

Views: 1515

Answers (2)

LPs
LPs

Reputation: 16223

You cant do that, obviously

Standard C11

6.2.4 Storage durations of objects

  1. An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type.

but you can use named one

#include <stdio.h>

enum my_enum
{
    a,
    b,
    c,
    MY_ENUM_MAX
};

void func(enum my_enum value)
{
    printf("%d\n", value);
}

int main(void)
{
    func(a);
    func(b);
}

or you can typedef it

#include <stdio.h>

typedef enum
{
    a,
    b,
    c,
    MY_ENUM_MAX
}my_enum;

void func (my_enum value)
{
    printf("%d\n", value);
}

int main(void)
{
    func(a);
    func(b);
}

Upvotes: 1

Badda
Badda

Reputation: 1369

As said in the comments:

Use a named enum. You can never have two anonymous enums be considered the same type.

Edit this post if something can be added to make it more useful or clear.

Upvotes: 2

Related Questions