Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:03:03+00:00 2026-05-25T22:03:03+00:00

Not that I would ever write the code like the following in my professional

  • 0

Not that I would ever write the code like the following in my professional work, the following code is legal and compiles without warnings in c++ and c:

#include <stdlib.h>

typedef struct foo { int foo; } foo;

foo * alloc_foo () {
   return (struct foo*) malloc(sizeof(foo));
}   

struct foo * alloc_struct_foo () {
   return (foo*) malloc(sizeof(struct foo));
}   


foo * make_foo1 (int val) {
   foo * foo = alloc_struct_foo (); 
   foo->foo = 0;
   return foo;
}

struct foo * make_foo2 (int val) {
   struct foo * foo = alloc_foo();
   foo->foo = 0;
   return foo;
}

What makes this legal and unambiguous in C is section 6.2.3 of the C standard:

6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers (label names; tags of structures, unions, and enumerations; members of structures or unions; and ordinary identifiers).

Note that thanks to label names living in their own name spaces, I could have made the code even more obfuscated by using a label foo somewhere.

Add the following and the code does not compile:

int foo (foo * ptr) {
   return ++ptr->foo;
}   

So, two questions, one related to C and C++ and the other, C++.

  • C/C++ question: Why can’t I define the function foo?
    It seems I should be able to define the function foo; function names and variable names are “ordinary identifiers”. But if I add that last little bit of code I get error: redefinition of 'foo' as different kind of symbol.
    Question: foo * foo; is perfectly legal, so why isn’t int foo (foo*); legal?

  • C++ question: How does this work at all in C++?
    The meaning of “name space” takes on a rather different meaning on in C++ than in C. I can’t find anything in the C++ standard that talks about the C concept of name spaces, which is what makes the above legal in C.
    Question: What makes this legal in C++ (chapter and verse preferred)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:03:04+00:00Added an answer on May 25, 2026 at 10:03 pm

    foo * foo; is perfectly legal, so why isn’t int foo (foo*); legal?

    Because there already is a type named foo in the same declaration context as your function. You cannot have a type and a function of the same name in the same scope.

    How does this work at all in C++?

    Because you are allowed to hide names in nested scopes. When you declare foo * foo, the first foo refers to the type. The second foo declares a variable — at that point, the type foo is hidden. Try declaring foo * baz after foo * foo, it should fail.

    struct foo {};
    
    void test() {
       foo * foo; // first `foo` is the type, second `foo` is the variable
       foo * baz; // first `foo` is the variable
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not that I would ever need to do this, but I want to understand
What's an example of something dangerous that would not be caught by the code
Not sure this is possible, but looking to write a script that would return
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to scan a directory for any assemblies that are not already
I would like to take a field and replace all characters that are not
Possible Duplicate: CSVs without quotes not working with fgetcsv I would like to start
I searched the Web and could not find anything that would show me a
I'm trying to install WMA DRM licenses files silently so that users would not
How would you rsync files that are not tracked by mercurial between two repositories?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.