Skip to Content.
Sympa Menu

c-semantics - [C-Semantics] function prototype scope

c-semantics AT lists.cs.illinois.edu

Subject: C Semantics in K Framework

List archive

[C-Semantics] function prototype scope


Chronological Thread 
  • From: Chucky Ellison <celliso2 AT illinois.edu>
  • To: c-semantics AT cs.illinois.edu
  • Subject: [C-Semantics] function prototype scope
  • Date: Sat, 18 Jun 2011 12:20:39 -0500
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/c-semantics>
  • List-id: C Semantics in K Framework <c-semantics.cs.illinois.edu>

What is the behavior of the following program?

int sub (int i, int array[i++]) {
return i;
}
int main() {
int array[10];
return sub(10, array);
}

GCC and ICC return 11; Clang returns 10.

I think the applicable paragraph from the standard is this:
(n1570 6.7.6.2:5) [speaking of array declarators] "If the size is an
expression that is not an integer constant expression: if it occurs in
a declaration at function prototype scope, it is treated as if it were
replaced by *; otherwise, each time it is evaluated it shall have a
value greater than zero."

I'm pretty sure this is "function prototype scope" and that "i++" is
not an integer constant expression, so it should be treated like a
"*". My understanding of the "*" in array declarators is fuzzy, but I
don't think they are allowed to be used in a definition, only in a
function declaration. Indeed, all of the compilers complain with an
error if I try:
int sub (int i, int array[*]) { ... }
Instead, doing this:
int sub (int i, int array[*]);
int sub (int i, int array[10]) { ... }
compiles and runs.

If i++ is supposed to be evaluated (although I don't know how, based
on the above 6.7.6.2:5), then when does it get evaluated? During
every call? At definition time? Are the side effects guaranteed to
occur? Where should I be looking in the standard instead of the
quotes above?

Does anyone know what is going on here?

-Chucky




Archive powered by MHonArc 2.6.16.

Top of Page