We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,011 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Macro name same as typedef'd type

My understanding of the C language is that the same identifier cannot be reused. However, the code below compiles on GCC without complaints, even when using the "-Wall -pedantic" flags. Is there something that I am missing? Does the standard say anything about functions/macros having the same name as typedef'd types?

#include <stdio.h>

typedef int error;

#define init() error var_error = -6

#define error() ((const error)var_error)

int main(void)
{
    init();
    printf("error() = %d\n", error());

    return 0;
}
2
Contributors
2
Replies
7 Hours
Discussion Span
9 Months Ago
Last Updated
3
Views
Question
Answered
_avishek
Light Poster
34 posts since Feb 2010
Reputation Points: 24
Solved Threads: 4
Skill Endorsements: 0

My understanding of the C language is that the same identifier cannot be reused.

That's correct, but only within the same name space (not the same as a C++ namespace). There are five listed name spaces in C:

  1. Label names (such as cases and goto labels).
  2. Structure, union, or enum tags.
  3. Structure or union members.
  4. Typedef names;
  5. Everything else.

With those name spaces in mind, it's easy to understand why the following is legal:

typedef struct foo { int foo; } foo;

In your code, the typedef error and the macro error are in different name spaces (the typedef falls under #4 and the macro under #5), so there's no direct conflict.

deceptikon
Challenge Accepted
Administrator
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

That's pretty neat! I did not know about these implied name spaces.

+5% cooler to you!

_avishek
Light Poster
34 posts since Feb 2010
Reputation Points: 24
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 9 Months Ago by deceptikon

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1011 seconds using 2.75MB