Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Using ternary and summing array

Thread Tools

Using ternary and summing array

Kaz Kylheku
Posts: n/a
On 2011-11-03, Kenneth Brody <(E-Mail Removed)> wrote:
> Note that my compiler creates a[] with zero members.
> However, my compiler acts "strange" on this point.
> #include <stdio.h>
> int a[];
> int main(void)
> {
> printf("%ld\n",(long)sizeof(a));
> }

Do you have some tools so you see what the size of a is in the translated
object file? (Is it in the object file?)

This might be a degenerate case which has no meaning due to a possible flaw in
the standard.

Under 6.9.2 we have this text:

A declaration of an identifier for an object that has file scope without an
initializer, and without a storage-class specifier or with the storage-class
specifier static, constitutes a tentative definition. If a translation unit
contains one or more tentative definitions for an identifier, and the
translation unit contains no external definition for that identifier, then the
behavior is exactly as if the translation unit contains a file scope
declaration of that identifier, with the composite type as of the end of the
translation unit, with an initializer equal to 0.

But an array cannot have an initializer equal to 0:

int a[] = 0; // invalid!

The second example for this section states the intent:

If at the end of the translation unit containing
int i[];
the array i still has incomplete type, the implicit initializer causes it to
have one element, which is set to zero on program startup.

This would be the case if the implicit initializer were { 0 }. The intent
is clear from the example, but for the text to bear it out, we have to
believe that "an initializer equal to zero" refers to a specific choice
of initializer such as { 0 }.

> Further investigation shows that my compiler seems to treat "int a[];" the
> same as "extern int a[];". Is that "legal"?

I would avoid this situation, since only a non-normative example gives it
a clear meaning.

Compiler writers, however, should pay careful attention to examples and

What does your compiler do if, at the end of the translation unit, you

int a[] = { 0 };

Reply With Quote
David Thompson
Posts: n/a
On Mon, 31 Oct 2011 07:46:25 -0400, James Kuyper
<(E-Mail Removed)> wrote:
> int three_five_sum(
> int a[],
> int n
> ){
> // body of function.
> }
> That looks like it defines a to be an array, but one of the confusing
> features of C is that the declaration of an array as a parameter to a
> function is automatically converted into a declaration of a pointer to
> the element type of that array. It's exactly the same as if I had
> written "int *a". Just as a matter of convention, I normally declare
> parameters which are pointers to the first element of an array using
> array[], while I write those which are pointers to a single object as
> *single_object. As far as I can tell, this convention is my own
> peculiarity, and not in wide use.

FWIW, I do the same when I get to choose the style.
Two is still far short of demonstrating wide use, though.

Further, if I can express simply enough the actual size expected or
used, I make it a comment:

void interdigitate_a_flooby (flooby * x);

void pair_items (left a [/*n*/], const right b [/*n*/], size_t n);
/* this one could be VLAs in C99 if change order */

void grommetize_blorbs (blorb b [/*g_min_blorb+tan(2pi)*/]);
/* ignore the global behind the curtain */

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
summing of array elements in for loop antsor VHDL 0 02-12-2012 06:42 AM
Excluding NAN entries when summing an array RS C++ 1 09-21-2006 08:45 PM
Excluding NAN entries when summing an array RS C++ 0 09-21-2006 02:03 PM
Microbenchmark: Summing over array of doubles Yaroslav Bulatov Python 9 08-03-2004 12:36 AM
Counting and summing elements in an array? Help! Alf P. Steinbach C++ 2 02-05-2004 09:27 AM