Declaration

M

marko

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?
 
A

aegis

marko said:
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'.
 
M

marko

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?
 
B

Barry Schwarz

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
 
R

Robert Gamble

Barry said:
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
 
D

Dietmar Schindler

Robert said:
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.
 
R

Robert Gamble

Dietmar said:
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
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top