By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,124 Members | 953 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,124 IT Pros & Developers. It's quick & easy.

Declaration

P: n/a
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?
Dec 31 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

marko wrote:
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

Dec 31 '06 #2

P: n/a
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?
Jan 1 '07 #3

P: n/a
On Sun, 31 Dec 2006 14:14:26 -0500, "marko" <ma***@validnow.com>
wrote:
>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
Jan 1 '07 #4

P: n/a
Barry Schwarz wrote:
On Sun, 31 Dec 2006 14:14:26 -0500, "marko" <ma***@validnow.com>
wrote:
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

Jan 1 '07 #5

P: n/a
Robert Gamble wrote:
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
Jan 5 '07 #6

P: n/a
Dietmar Schindler wrote:
Robert Gamble wrote:
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

Jan 5 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.