declaring variables with same names

S

stonerain

Hello folks,

here the code below gives me 400 as result but I couldn't quite understand what is going on here.

struct a and the array have the same name then inside the main function why sizeof chooses the array one? What is the rule here.

Thanks

#include <iostream>

int a[100];

struct a
{
int i;
};

int main()
{
std::cout << sizeof(a) << std::endl;
}
 
I

Ike Naar

Hello folks,

here the code below gives me 400 as result but I couldn't quite
understand what is going on here.
struct a and the array have the same name then inside the main
function why sizeof chooses the array one? What is the rule here.

Thanks

#include <iostream>

int a[100];

struct a
{
int i;
};

int main()
{
std::cout << sizeof(a) << std::endl;
}

From 9.1 [class.name]:
2 [...] If a class name is declared in a scope where a variable,
function, or enumerator of the same name is also declared, then
when both declarations are in scope, the class can be referred to
only using an elaborated-type-specifier (3.4.4).

So, because the name 'a' is used as a variable name (int a[100];),
the class 'struct a' can be referred to only using the name 'struct a'.

If the name 'a' were only used in the struct declaration (that is,
if the 'int a[100];' declaration were absent), then the class 'struct a'
could be referred to as either 'a' or 'struct a'.
 
J

James Kanze

here the code below gives me 400 as result but I couldn't quite
understand what is going on here.
struct a and the array have the same name then inside the main
function why sizeof chooses the array one? What is the rule here.
#include <iostream>
int a[100];
struct a
{
int i;
};
int main()
{
std::cout << sizeof(a) << std::endl;
}
"struct a" creates a typename, whereas "int a[100]" creates a variable
name. You can create variables with the same name as existing types, and
these hide the original types (you are not supposed to know and avoid all
those names of existing types in all those included headers).

It isn't a question of whether "a" is a typename or not; only
class or enum types can conflict with other names in this way.
And it has nothing to do with the names in the standard library:
all of the names in the standard library are in namespace std,
and there are more functions than types anyway. The motivation
is simply C compatibility: in C, the name directly following
a struct, union or an enum is called a tag, and is in a separate
symbol table than other names. This works because in C, you
must precede such names with the corresponding elaborated type
specifier. If you look in <sys/stat.h> on a Posix machine, for
example, you'll find:

struct stat { /* ... */ };
int stat( /* ... */ );

A function with the same name as the structure.

One should, of course, avoid using the same names in new C++.
But C++ does have to be able to include the Posix headers (which
were there before there was C++).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top