473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Are int a; int a(); int a=0; the same?

Hi,

I'm wondering if the following three statements are the same or only
the last two are the same?

int a;
int a();
int a=0;

Thanks,
Peng
Nov 18 '07 #1
13 2087
"Pe*******@gmail.com" <Pe*******@gmail.comwrote:
I'm wondering if the following three statements are the same or only
the last two are the same?

int a;
int a();
int a=0;
No. This sounds remarkably like a homework/test question. Ask yourself,
what will be the value of 'a' in the first case? What will be it's value
in the last case?

As for the middle line, compare a function declaration to a variable
definition...
Nov 18 '07 #2
<Pe*******@gmail.comwrote in message
news:fa**********************************@a28g2000 hsc.googlegroups.com...
Hi,

I'm wondering if the following three statements are the same or only
the last two are the same?

int a;
int a();
int a=0;
You missed one
int a(0);

The three you show do different things.

The first one declares a as an interger which is unitialized. Can contain
any value. Be careful, some compilers will initialize variables to 0 in
debug mode but not release mode sometimes causing hard to find errors.

The second one declares a function called a accepting no parameters and
retuning an integer.

The third one declares a as in integer and initializes it to zero.

The fourth one also declares a as an integer and initializes it to zero.

Note:
int a;
a = 0;

is different than
int a = 0;

The first case uses the assignment operator. The second case uses the
constructor.
Nov 18 '07 #3
On Nov 18, 5:03 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<PengYu...@gmail.comwrote in message
news:fa**********************************@a28g2000 hsc.googlegroups.com...
I'm wondering if the following three statements are the same or only
the last two are the same?
int a;
int a();
int a=0;
You missed one
int a(0);
The three you show do different things.
The first one declares a as an interger which is unitialized.
Can contain any value. Be careful, some compilers will
initialize variables to 0 in debug mode but not release mode
sometimes causing hard to find errors.
In debug mode, I'd expect something like 0xdeadbeef.

And of course, it depends where the declarations are placed. If
they are at namespace scope, then the first one is zero
initialized.
The second one declares a function called a accepting no
parameters and retuning an integer.
The third one declares a as in integer and initializes it to
zero.
The fourth one also declares a as an integer and initializes
it to zero.
Note:
int a;
a = 0;
is different than
int a = 0;
The first case uses the assignment operator. The second case
uses the constructor.
If you throw constructors into it, then "int a(a);" and "int
a=0;" are different as well:-). Still, the first is assignment,
and the second initialization, and there are cases, even with
int, where this makes a difference:

switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;

case 1:
int b ; // Legal...
b = 0 ;
break ;
}

(How's that for adding to the confusion:-)?)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '07 #4

<Pe*******@gmail.comwrote in message
news:fa**********************************@a28g2000 hsc.googlegroups.com...
Hi,

I'm wondering if the following three statements are the same
No.
or only
the last two are the same?
No.
>
int a;
Defines a type 'int' object, which has an indeterminate
value if at block scope, or has a value of zero if at
file scope.
int a();
Declares a function named 'a', which has no arguments
and returns type 'int'.
int a=0;
Defines a type 'int' object, and initializes its value to zero.
Another form is:

int a(0);
-Mike
Nov 18 '07 #5
On 2007-11-18 06:41:10 -0500, James Kanze <ja*********@gmail.comsaid:
>
switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;

case 1:
int b ; // Legal...
b = 0 ;
break ;
}

(How's that for adding to the confusion:-)?)
This is so weird. Is there a reason for initiations in switch
statements to be illegal?

However, I noticed that if you put braces around the case blocks, then
it compiles fine under g++ 4.0.1.

Some other weird observations below.

The following is legal (under g++ 4.0.1).
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // ok too!
break;
}

The following is illegal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // illegal here!
break;
case 2:
int c; // ok
c = 1;
break;
}

The following is legal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b; // ok
break;
case 2:
int c = 1; // ok
c = 1;
break;
}

It seems like the rule is that you cannot initialize a variable in a
declaration in a case block that is followed by another case block.

--

-kira

Nov 19 '07 #6
Thannks Kira. This is interesting and Strange!

~
Prathap
Pivot Systems

On Nov 19, 7:39 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-11-18 06:41:10 -0500, James Kanze <james.ka...@gmail.comsaid:
switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;
case 1:
int b ; // Legal...
b = 0 ;
break ;
}
(How's that for adding to the confusion:-)?)

This is so weird. Is there a reason for initiations in switch
statements to be illegal?

However, I noticed that if you put braces around the case blocks, then
it compiles fine under g++ 4.0.1.

Some other weird observations below.

The following is legal (under g++ 4.0.1).
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // ok too!
break;

}

The following is illegal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // illegal here!
break;
case 2:
int c; // ok
c = 1;
break;

}

The following is legal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b; // ok
break;
case 2:
int c = 1; // ok
c = 1;
break;

}

It seems like the rule is that you cannot initialize a variable in a
declaration in a case block that is followed by another case block.

--

-kira
Nov 19 '07 #7
On Sun, 18 Nov 2007 21:39:28 -0500, Kira Yamato wrote:
On 2007-11-18 06:41:10 -0500, James Kanze <ja*********@gmail.comsaid:
> switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;

case 1:
int b ; // Legal...
b = 0 ;
break ;
}

(How's that for adding to the confusion:-)?)

This is so weird. Is there a reason for initiations in switch
statements to be illegal?
A label (including the case labels) must be followed by a statement.

Apparently, this compiler does not believe
that "int a = 0;" is a statement but that "int b;" is.

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 19 '07 #8
A label (including the case labels) must be followed by a statement.
>
Apparently, this compiler does not believe
that "int a = 0;" is a statement but that "int b;" is.
I thought that "int b;" is a statement in C++ contrary to C, such that
it allows you to declare variables anywhere inside a function etc. And
as I see it, it doesn't explain Kira's examples either.

(I blame James Kanze for confusing me - but it must have to do
something with assignment versus initialization)
Nov 19 '07 #9
Joel Yliluoma wrote:
A label (including the case labels) must be followed by a statement.

Apparently, this compiler does not believe
that "int a = 0;" is a statement but that "int b;" is.
NO the issue is not that but the fact that it is not allowed
to JUMP over an initalization into the scope of a variable:

switch(i) {
case 0:
int j = 5;
break;
case 1:
// j is still in scope here, but if we jump
// to the case 1 label, we miss it's initialization
j++;
};

The fix is to put extra braces in:
switch(i) {
case 0: {
int j = 5;
break;
}
case 1:
// OK, no variable initialization bypassed.

Nov 19 '07 #10
On Nov 19, 3:39 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-11-18 06:41:10 -0500, James Kanze <james.ka...@gmail.comsaid:
switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;
case 1:
int b ; // Legal...
b = 0 ;
break ;
}
(How's that for adding to the confusion:-)?)
This is so weird. Is there a reason for initiations in switch
statements to be illegal?
It's the result of a more general rule: you're not allowed to
jump around a non-trivial initialization: either an explicit
initialization or a definition with a type which has a
non-trivial constructor. Thus, for example, something like:

goto foo ;
std::ifstream in( "filename" ) ;
foo:
int i ;
in >i ;

is illegal. In this case, it's also quite obvious that it
should be: you're attempting to use "in" without having invoked
its constructor, which can only cause problems. And of course:

goto foo ;
int i = 43 ;
foo :
std::cout << i ;

suffers from the same problem; you're attempting to use an
uninitialized i.

The apparently strange behavior in my example with switch is due
to this rule interacting with other particularities of the
language: the fact that the semantics of switch are exactly
those of goto, for example (think of what happens if you leave
out a break), and the fact that something like:

switch ( something ) {
int a ;
case 0 :
a = 1 ;
// ...
break ;

case 2:
a = 2 ;
// ...
break ;
}

is legal C, and the authors probably didn't want to break it in
C++.

The final result is that in almost every case, you should wrap
the actions in each case in {...}.
However, I noticed that if you put braces around the case
blocks, then it compiles fine under g++ 4.0.1.
Yes, because you don't jump around the initialization; the
variable isn't visible in the other cases.
Some other weird observations below.
The following is legal (under g++ 4.0.1).
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // ok too!
break;
}
Correct. What makes the code illegal is the presence of a case
after the the definition.
The following is illegal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // illegal here!
break;
case 2:
int c; // ok
c = 1;
break;

}
The following is legal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b; // ok
break;
case 2:
int c = 1; // ok
c = 1;
break;
}
It seems like the rule is that you cannot initialize a
variable in a declaration in a case block that is followed by
another case block.
That's the general idea. To put it more exactly, there is no
such thing as a "case block", and case labels are considered as
a target of a goto. The "block" is thus that of the switch, and
the rule is that you're not allowed a goto over a definition
with a non-trivial initialization to a point where the variable
being defined is still legal.

In practice, in all but the most trivial cases (every case
consists of just a single assignment or return), you should
probably systematically wrap all cases in a block, e.g.:

switch ( something )
{
case 0 :
{
// ...
}
break ;

case 1 :
{
// ...
}
break ;
}

etc.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 19 '07 #11
On Nov 19, 12:41 pm, Ioannis Gyftos <ioannis.gyf...@gmail.comwrote:
A label (including the case labels) must be followed by a statement.
Apparently, this compiler does not believe
that "int a = 0;" is a statement but that "int b;" is.
Which is, of course, ridiculous. Both are statements.
I thought that "int b;" is a statement in C++ contrary to C,
such that it allows you to declare variables anywhere inside a
function etc.
It is a statement both in C and in C++. And modern C also
allows declaring variables in pretty much all of the places C++
does.
And as I see it, it doesn't explain Kira's examples either.
(I blame James Kanze for confusing me - but it must have to do
something with assignment versus initialization)
The assignment is actually irrelevant: it's a definition with
initialization vs. one without. C++ doesn't allow jumping over
or around initialization, and the semantics of switch are the
same as those of goto.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 19 '07 #12
On 2007-11-19 08:33:30 -0500, James Kanze <ja*********@gmail.comsaid:
On Nov 19, 3:39 am, Kira Yamato <kira...@earthlink.netwrote:
>On 2007-11-18 06:41:10 -0500, James Kanze <james.ka...@gmail.comsaid:
>>switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;
>>case 1:
int b ; // Legal...
b = 0 ;
break ;
}
>>(How's that for adding to the confusion:-)?)
>This is so weird. Is there a reason for initiations in switch
statements to be illegal?

It's the result of a more general rule: you're not allowed to
jump around a non-trivial initialization: either an explicit
initialization or a definition with a type which has a
non-trivial constructor. Thus, for example, something like:

goto foo ;
std::ifstream in( "filename" ) ;
foo:
int i ;
in >i ;

is illegal. In this case, it's also quite obvious that it
should be: you're attempting to use "in" without having invoked
its constructor, which can only cause problems. And of course:

goto foo ;
int i = 43 ;
foo :
std::cout << i ;

suffers from the same problem; you're attempting to use an
uninitialized i.

The apparently strange behavior in my example with switch is due
to this rule interacting with other particularities of the
language: the fact that the semantics of switch are exactly
those of goto, for example (think of what happens if you leave
out a break), and the fact that something like:

switch ( something ) {
int a ;
case 0 :
a = 1 ;
// ...
break ;

case 2:
a = 2 ;
// ...
break ;
}

is legal C, and the authors probably didn't want to break it in
C++.

The final result is that in almost every case, you should wrap
the actions in each case in {...}.
>However, I noticed that if you put braces around the case
blocks, then it compiles fine under g++ 4.0.1.

Yes, because you don't jump around the initialization; the
variable isn't visible in the other cases.
>Some other weird observations below.
>The following is legal (under g++ 4.0.1).
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // ok too!
break;
}

Correct. What makes the code illegal is the presence of a case
after the the definition.
>The following is illegal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b = 1; // illegal here!
break;
case 2:
int c; // ok
c = 1;
break;

}
>The following is legal.
switch(something)
{
case 0:
int a; // ok
a = 1;
break;
case 1:
int b; // ok
break;
case 2:
int c = 1; // ok
c = 1;
break;
}
>It seems like the rule is that you cannot initialize a
variable in a declaration in a case block that is followed by
another case block.

That's the general idea. To put it more exactly, there is no
such thing as a "case block", and case labels are considered as
a target of a goto. The "block" is thus that of the switch, and
the rule is that you're not allowed a goto over a definition
with a non-trivial initialization to a point where the variable
being defined is still legal.

In practice, in all but the most trivial cases (every case
consists of just a single assignment or return), you should
probably systematically wrap all cases in a block, e.g.:

switch ( something )
{
case 0 :
{
// ...
}
break ;

case 1 :
{
// ...
}
break ;
}

etc.
Thanks for the detail explanation.

--

-kira

Nov 19 '07 #13
On Nov 19, 2:43 pm, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Nov 19, 3:39 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-11-18 06:41:10 -0500, James Kanze <james.ka...@gmail.comsaid:
switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;
> case 1:
int b ; // Legal...
b = 0 ;
break ;
}
>(How's that for adding to the confusion:-)?)
This is so weird. Is there a reason for initiations in switch
statements to be illegal?
It's the result of a more general rule: you're not allowed to
jump around a non-trivial initialization: either an explicit
initialization or a definition with a type which has a
non-trivial constructor.
You mean non-POD type: the standard defines this in terms of
POD'ness.
So I see. I wonder if this was added to the standard, or if it
is just my memory playing tricks. (I haven't had the occasion
to actually look at this passage since the ARM. Many years ago.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 20 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: bq | last post by:
In the code int a; int b = -1; does ANSI C guarantee that "b" is located in memory right after "a" so that "a" refers to "b"? Thanks. bq
2
by: Daniel Goldman | last post by:
Hi, Any advice about both a BinaryReader and BinaryWriter containing same FileStream at same time? Like: Stream fs = new FileStream("output.dbf", FileMode.Create); BinaryReader br = new...
4
by: don | last post by:
I have two existing interfaces having methods with same names. Now I have to implement both intrfaces in one class. Is there any way I could implement methods with same names in both interfaces...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
20
by: subramanian | last post by:
Hello I have a doubt in the following piece of code: int a; printf("a=%p\n", a); printf("&a=%p\n", &a); these printf statements print the same value for both 'a' and '&a". I tried in...
8
by: jaibux | last post by:
Everybody knows how to open (or clone) the same page that you are viewing in a new browser window by CTRL+N or via File->New Window. the question is how to PREVENT to open the SAME WEB APPLICATION...
16
by: Zytan | last post by:
MSDN says: "The ref and out keywords are treated differently at run- time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument...
28
by: SzH | last post by:
Suppose that there is a program that takes two files as its command line arguments. Is there a (cross platform) way to decide whether the two files are the same? Simple string comparison is not...
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.