Connecting Tech Pros Worldwide Forums | Help | Site Map

Declaration

marko
Guest
 
Posts: n/a
#1: Dec 31 '06
I was reading page 131 in K&R C 2nd ed. book.

In page 63,

"The commas that seperate function arguments, variables in declarations, etc
are not comma operators, and do not guarantee left to right evaluation."

and bottom of page 131 there's example of
struct rect r, *rp = &r;
also in page 216, near bottom,
int i, *pi, *const cpi = &i;

my question is if comma in declaration does not gurantee left to right
evaluation, how did in examples latter valuables declared and intialized to
value of previous valuables on same line?

couln't the *rp declared before r is declared?



aegis
Guest
 
Posts: n/a
#2: Dec 31 '06

re: Declaration



marko wrote:
Quote:
I was reading page 131 in K&R C 2nd ed. book.
>
In page 63,
>
"The commas that seperate function arguments, variables in declarations, etc
are not comma operators, and do not guarantee left to right evaluation."
>
and bottom of page 131 there's example of
struct rect r, *rp = &r;
also in page 216, near bottom,
int i, *pi, *const cpi = &i;
>
my question is if comma in declaration does not gurantee left to right
evaluation, how did in examples latter valuables declared and intialized to
value of previous valuables on same line?
>
couln't the *rp declared before r is declared?
Because they are using a value that is a pointer to an object
and not a value of an object.

int foo = 10;

&foo; <= value of this expression is a pointer to object 'foo'
foo; <= value of this expression is the value stored in the object
'foo'

Understand?

Additionally, you are saying 'valuables' when you mean to say 'value'.

--
aegis

marko
Guest
 
Posts: n/a
#3: Jan 1 '07

re: Declaration


ahh.. not really
I was baffled by order..

um..ok

if left to right evaluation is not guranteed, statement like
struct rect r, *rp=&r;
couldbe evaluated order below?
struct rect *rp=&r;
struct rect r;
depends on different situation since order is undefined.

instead of

struct rect r;
sturct rect *rp=&r;

the way supposed to be?


Barry Schwarz
Guest
 
Posts: n/a
#4: Jan 1 '07

re: Declaration


On Sun, 31 Dec 2006 14:14:26 -0500, "marko" <marko@validnow.com>
wrote:
Quote:
>I was reading page 131 in K&R C 2nd ed. book.
>
>In page 63,
>
>"The commas that seperate function arguments, variables in declarations, etc
>are not comma operators, and do not guarantee left to right evaluation."
>
>and bottom of page 131 there's example of
struct rect r, *rp = &r;
>also in page 216, near bottom,
int i, *pi, *const cpi = &i;
>
>my question is if comma in declaration does not gurantee left to right
>evaluation, how did in examples latter valuables declared and intialized to
>value of previous valuables on same line?
>
>couln't the *rp declared before r is declared?
>
Even though the comma is not a comma operator, section 6.2.1-4 states
that the scope of an identifier is determined by the placement of its
declaration. 6.2.1-7 states that the scope begins just after the
completion of its declarator. The comma marks the end of the
declaration of r and the start of rp. r is therefore in scope at the
time rp is declared and the compiler is not allowed reverse the order.


Remove del for email
Robert Gamble
Guest
 
Posts: n/a
#5: Jan 1 '07

re: Declaration


Barry Schwarz wrote:
Quote:
On Sun, 31 Dec 2006 14:14:26 -0500, "marko" <marko@validnow.com>
wrote:
>
Quote:
I was reading page 131 in K&R C 2nd ed. book.

In page 63,

"The commas that seperate function arguments, variables in declarations, etc
are not comma operators, and do not guarantee left to right evaluation."

and bottom of page 131 there's example of
struct rect r, *rp = &r;
also in page 216, near bottom,
int i, *pi, *const cpi = &i;

my question is if comma in declaration does not gurantee left to right
evaluation, how did in examples latter valuables declared and intialized to
value of previous valuables on same line?

couln't the *rp declared before r is declared?
>
Even though the comma is not a comma operator, section 6.2.1-4 states
that the scope of an identifier is determined by the placement of its
declaration. 6.2.1-7 states that the scope begins just after the
completion of its declarator. The comma marks the end of the
declaration of r and the start of rp. r is therefore in scope at the
time rp is declared and the compiler is not allowed reverse the order.
Additionally, 6.8p3 states that initializers of automatic variables are
evaluated in the order they appear meaning that the following code must
result in x being assigned the value of 0 and y being assigned the
value of 1 and not vice versa:

int f(void) {static int i; return i++;}
int main (void) {
int x=f(), y=f();
return 0;
}

Since there is no sequence point between the declarators though, the
following would be undefined:

int i = 1;
int x = i++, y = i++;

Robert Gamble

Dietmar Schindler
Guest
 
Posts: n/a
#6: Jan 5 '07

re: Declaration


Robert Gamble wrote:
Quote:
Since there is no sequence point between the declarators though, the
following would be undefined:
>
int i = 1;
int x = i++, y = i++;
N1124, 6.8 Statements and blocks
....
4 ... Each of the following is a full expression: an initializer; ...
The end of a full expression is a sequence point.

--
Dietmar Schindler
Robert Gamble
Guest
 
Posts: n/a
#7: Jan 5 '07

re: Declaration


Dietmar Schindler wrote:
Quote:
Robert Gamble wrote:
Quote:
Since there is no sequence point between the declarators though, the
following would be undefined:

int i = 1;
int x = i++, y = i++;
>
N1124, 6.8 Statements and blocks
...
4 ... Each of the following is a full expression: an initializer; ...
The end of a full expression is a sequence point.
I stand corrected.

Robert Gamble

Closed Thread