-1

I am attempting to do this for a smaller example so that I can do it for a larger sample.

Here is my attempted code:

int counts[0];
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
    counts[0] += numbers[i];

printf("%d ", counts[0]);

This should yield 55, but my output is only 14. There is an issue with how I am setting up the numbers array, but I am not quite sure how to fix this. Any help is appreciated, thank you.

hkj447
  • 677
  • 7
  • 21
  • 2
    The array `int counts[0]` has no size. You just need `int counts = 0;` and then use `counts` instead of `counts[0]` in the rest of it. – Weather Vane Jul 03 '19 at 19:02
  • `counts[0]` should be intialized to some number – DaCurse Jul 03 '19 at 19:02
  • 2
    Try `int counts[1] = {0};` instead of `int counts[0];` An array of size 0 is not useful. – user3386109 Jul 03 '19 at 19:07
  • 1
    [ISO/IEC 9899:TC2:6.7.5.2](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf) – Agnius Vasiliauskas Jul 03 '19 at 19:08
  • @AgniusVasiliauskas the comments under that answer indicate that a compiler is allowed to build a zero-length array, as long as a diagnostic message is issued. But of course, a zero length array cannot be indexed by `[0]` as it has no elements. – Weather Vane Jul 03 '19 at 19:13
  • 1
    @WeatherVane So what ? Many things are allowed for a compiler by a standard, because **they are not dis-allowed by it**. But this makes these "allowed" things undocumented by a standard, thus implying IDB (implementation-defined behavior) or UB (undefined-behavior). None of these behaviors are really useful for a developer. – Agnius Vasiliauskas Jul 03 '19 at 19:46
  • https://math.stackexchange.com/questions/2260/proof-for-formula-for-sum-of-sequence-123-ldotsn – Neil Jul 03 '19 at 19:49
  • @AgniusVasiliauskas you since changed the [answer link](https://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c/9723093#9723093) but I supposed you had posted it for a reason, and the comments under the answer said it was incorrect. It wasn't a useful discussion for a beginner, but I didn't notice when you edited the comment to a different link. – Weather Vane Jul 03 '19 at 19:57
  • FWIW: Zero-length arrays do have some great uses in C. I use them a lot in binary packing and network communications (https://stackoverflow.com/questions/14643406/whats-the-need-of-array-with-zero-elements) – abelenky Jul 03 '19 at 20:41
  • 1
    @abelenky no they don't. All that garbage has undefined behaviour. That's why c99+ has the flexible array member – Antti Haapala -- Слава Україні Jul 04 '19 at 02:51

1 Answers1

2
int counts[0];   // This declares an array that holds ZERO elements!  No place to store a value.
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
    counts[0] += numbers[i];

printf("%d ", counts[0]);

To fix it:

int counts = 0;   // Declare a variable, and start it at 0
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
    counts += numbers[i];

printf("%d ", counts);
abelenky
  • 63,815
  • 23
  • 109
  • 159