external variable giving error

V

venkatarao0

file1.cpp
file2.cpp
#include<iostream> int
arry[10] ={0};
usging namespace std;
extern int arry[];

main()
{
cout << sizeof(arry); // hear its giving compiler error
}


Same program working fine in .c file( C program) and giving '0' size

why its giving compiler error in C++. Please give reply to me Thanks
in advance

Thanks & Regards,
Venkat.
 
O

oalfishcivil

file1.cpp
file2.cpp
#include<iostream>                                                int
arry[10] ={0};
usging namespace std;
extern int arry[];

main()
{
cout << sizeof(arry); // hear its giving compiler error

}

Same program working fine in .c file( C program) and giving '0' size

why  its giving compiler error in C++. Please give reply to me Thanks
in advance

Thanks & Regards,
Venkat.

I am not sure what you have in your file1.cpp and file2.cpp. I just
compiled the following which is copied from your thread with
codeblocks
#include<iostream>
using namespace std;
int arry[10] ={0};
extern int arry[];

int main()
{
cout << sizeof(arry); // hear its giving compiler error
return 0;
}
I didn't get any error at all. I am not sure if you are that using
that external variable in other files.If so, I think you need to
declare it in all those files like extern int arry[].
oalfishcivil
 
W

WANG Cong

file1.cpp
file2.cpp
#include<iostream> int
arry[10] ={0};
usging namespace std;
extern int arry[];

It should be extern int arry[10];
main()
{
cout << sizeof(arry); // hear its giving compiler error
}


Same program working fine in .c file( C program) and giving '0' size

Are you sure?

If you declare an external array like that, you _can_ use it, but you
_can't_ know its size.
why its giving compiler error in C++. Please give reply to me Thanks
in advance

Because the compiler can't see the size of that external array, unless
you declare it in this file.
 
J

Juha Nieminen

using namespace std;

Yes, I'm a nitpicking bastard, but I simply cannot resist. You wrote
21 characters (including the newline) so that you could later save
writing 5 characters (ie. "std::") here:
cout << sizeof(arry); // hear its giving compiler error

In total you thus saved writing -16 characters. There aren't any other
benefits that I can see either.
 
J

James Kanze

file1.cpp
file2.cpp
#include<iostream> int
arry[10] ={0};
usging namespace std;
extern int arry[];
It should be extern int arry[10];

Both are acceptable. In one case, the type would be incomplete
(except that in his case, there is a preceding declaration which
specifies the dimension), in the other not.
Are you sure?

Probably the only relevant question. There are a couple of
problems with his code, which mean that we can't actually try it
and see what is going on. To start with, he mentions two files
(file1.cpp and file2.cpp), but doesn't say which code is where.
And if you just copy/paste his code, it won't compile, but for
reasons completely unrelated to "arry"---there's no return type
for main. Fix that, and it should work fine, outputting the
equivalent of 10*sizeof(int). Since he's mentionned two files,
I doubt that that's what he tried. But he hasn't given us any
real hint about the code which actually causes the problem.
If you declare an external array like that, you _can_ use it,
but you _can't_ know its size.

Well, *you* can. But the compiler can't:).
Because the compiler can't see the size of that external
array, unless you declare it in this file.

Which he did in his example code.
 
V

venkatarao0

Thanks for giving reply
i am giving the question ones again

file1.cpp
extern int arry[];
main()
{
cout << sizeof(arry); // hear its giving compiler error
}

and
file2.cpp
arry[10] ={0};
actual question was like this i posted wrongly

same program working in in .c file( C program) in visual studio 6.0
and giving '0' out put
but not working in C++ give me the solution.
 
W

WANG Cong

James said:
file1.cpp
file2.cpp
#include<iostream> int
arry[10] ={0};
usging namespace std;
extern int arry[];
It should be extern int arry[10];

Both are acceptable. In one case, the type would be incomplete
(except that in his case, there is a preceding declaration which
specifies the dimension), in the other not.

True, it *is* acceptable, but again, if you use the one from OP,
the compiler can't know its size, because they are in different
translate unit.
Probably the only relevant question. There are a couple of
problems with his code, which mean that we can't actually try it
and see what is going on. To start with, he mentions two files
(file1.cpp and file2.cpp), but doesn't say which code is where.
And if you just copy/paste his code, it won't compile, but for
reasons completely unrelated to "arry"---there's no return type
for main. Fix that, and it should work fine, outputting the
equivalent of 10*sizeof(int). Since he's mentionned two files,
I doubt that that's what he tried. But he hasn't given us any
real hint about the code which actually causes the problem.

No way! Any C++ compiler should not be *that* stupid! OP's
problem has *nothing* related with main, forget his main, focus
on that array!

Well, *you* can. But the compiler can't:).

It can. Try my example:

% cat test2.cpp

int array[10] = {0};

% cat test1.cpp
#include <iostream>

using namespace std;

extern int array[];

int main()
{
array[0] = 1; //[1]
cout<<sizeof array<<endl; //[2]
return 0;
}

% g++ -Wall -W -ansi -o test test2.cpp test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:10: error: invalid application of ‘sizeof’ to incomplete type ‘int
[]’

As you can see, there is *no* errors of line [1], that is exactly
what I said, you can use it, but you can't know its size.
 
W

WANG Cong

Thanks for giving reply
i am giving the question ones again

<snip>
Please don't only reply to me, reply to the news group.

same program working in in .c file( C program) in visual studio 6.0
and giving '0' out put
but not working in C++ give me the solution.

In C it should also be wrong, gcc can prove this. VS sucks here, there
is no size for declaration arry[], even zero!
 
J

James Kanze

James said:
(e-mail address removed) wrote:
file1.cpp
file2.cpp
#include<iostream> int
arry[10] ={0};
usging namespace std;
extern int arry[];
It should be extern int arry[10];
Both are acceptable. In one case, the type would be
incomplete (except that in his case, there is a preceding
declaration which specifies the dimension), in the other
not.
True, it *is* acceptable, but again, if you use the one from
OP, the compiler can't know its size, because they are in
different translate unit.

Except that they weren't in the code he posted. Everything was
in one translation unit.
No way! Any C++ compiler should not be *that* stupid! OP's
problem has *nothing* related with main, forget his main,
focus on that array!

Be that stupid in what respect? The compilers I've used don't
accept his code because there is no return type on main. If I
add the return type, they accept it.
It can. Try my example:
% cat test2.cpp
int array[10] = {0};
% cat test1.cpp
#include <iostream>
using namespace std;
extern int array[];
int main()
{
array[0] = 1; //[1]
cout<<sizeof array<<endl; //[2]
return 0;
}
% g++ -Wall -W -ansi -o test test2.cpp test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:10: error: invalid application of ‘sizeof’ to incomplete type ‘int
[]’

Which is what I just said. The compiler can't know its size
(even if you know it).

Actually, the standard requires a diagnostic in this case, even
if the compiler does know its size (and some more advanced
compilers can).
As you can see, there is *no* errors of line [1], that is
exactly what I said, you can use it, but you can't know its
size.

The above *isn't* the code he posted.
 
J

James Kanze

Thanks for giving reply
i am giving the question ones again
file1.cpp
extern int arry[];
main()
{
cout << sizeof(arry); // hear its giving compiler error
}
and
file2.cpp
arry[10] ={0};
actual question was like this i posted wrongly
same program working in in .c file( C program) in visual studio 6.0
and giving '0' out put
but not working in C++ give me the solution.

It's not legal C++ (nor C), and I've never seen a compiler which
didn't complain about it (but I've not got access to VC++---or
even to any version of VC++ at the moment, to try it). The C++
standard (and before that, the various documents which served as
reference) requires an error message. (Two, actually: one for
the missing return type of main, and the other for sizeof on an
incomplete type.)
 
W

WANG Cong

James said:
James said:
(e-mail address removed) wrote:
file1.cpp
file2.cpp
#include<iostream>
#int
arry[10] ={0};
usging namespace std;
extern int arry[];
It should be extern int arry[10];
Both are acceptable. In one case, the type would be
incomplete (except that in his case, there is a preceding
declaration which specifies the dimension), in the other
not.
True, it *is* acceptable, but again, if you use the one from
OP, the compiler can't know its size, because they are in
different translate unit.

Except that they weren't in the code he posted. Everything was
in one translation unit.


No, it's not, if you are right, why OP mentioned two files instead
of one?


Be that stupid in what respect? The compilers I've used don't
accept his code because there is no return type on main. If I
add the return type, they accept it.


Of course, the compiler should distinguish the error of that
array declaration and the error of main. Obviously, OP's problem
is the former one.

% g++ -Wall -W -ansi -o test test2.cpp test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:10: error: invalid application of ‘sizeof’ to incomplete type
‘int
[]’

Which is what I just said. The compiler can't know its size
(even if you know it).

Actually, the standard requires a diagnostic in this case, even
if the compiler does know its size (and some more advanced
compilers can).

Sure, OP exactly wants this answer, nothing related with main().

The above *isn't* the code he posted.

Strictly speaking, no, it's not exactly the same. BUT, it is exactly
what OP meant.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top