0

Program generates a core dump on Linux, but works fine on Windows. Any idea why?

#include <stdio.h>

int main() {
    int i, n;
    int count[n];
    int total;
    int value;
    int d;

    printf("Enter the length of array: ");
    scanf("%d", &n);

    //printf ("total of array is %4d \n", n);

    for (i=0; i<=n-1 ; i++ ) {
        printf("Enter the number %d: ", i);
        scanf("%d", &count[i]);
        //  printf ("total of array is %4d \n", n);
    }

    //printf ("total of array is %4d \n", n);

    value = totalcalc( count, n);        
    printf ("total of array is %3d \n", value);

    scanf ("%d", &d);
}

int totalcalc(int count1[], int j)
{
    int  i, total, value;
    //printf (" Entered into function, value of j is %d \n", j);
    value = 0;
    for (i=0; i<=j-1;i++ ) {
        value = value + count1[i];
        //printf ("the value is %d\n", value);
    }
    return value;
}
jweyrich
  • 31,198
  • 5
  • 66
  • 97
devnp
  • 93
  • 2
  • 9

3 Answers3

9

This part is very dubious:

int i, n;
int count[n];

n is clearly unitialized and you're allocating an array of size n.

If you want a dynamic-sized array you could do this:

int* count;
printf("Enter the length of array: ");
scanf("%d", &n);

count = malloc(n * sizeof(int)); // dynamically allocate n ints on heap

value = totalcalc( count, n);

printf ("total of array is %3d \n", value);

scanf ("%d", &d);

free(count); // free memory
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I want to keep the variable so I can use any value in n and n will decide the length of array. So if I want to make array of length 3 then I will use n=3 and if I want to keep array length 10 then I will use n=10. I am using scanf to get value of n. – devnp Oct 30 '12 at 16:33
  • It's also good to check whether the input number is not negative (or 0), and the allocation did not fail. +1 nevertheless – jweyrich Oct 30 '12 at 16:38
  • I am new to C and learning it so it will be great if you can explain it bit more, for example what does int* count means and what is count = malloc(n * sizeof(int)) doing? – devnp Oct 30 '12 at 17:14
  • 2
    @user1504633: `int *count` declares a pointer-to-int, which is designed to hold a memory address that points to a block of memory large enough to store an `int`. The `malloc` function allocates the specified amount of bytes, and returns a pointer to the newly allocated memory block. And `n * sizeof(int)`, multiplies `n` by the size of `int` (which may vary depending on aspects of hardware, system, and even compilation flags). We'd also like to recommend you **[some good books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)**. – jweyrich Oct 30 '12 at 17:28
7

Because int count[n] was declared before n was properly initialized.

jweyrich
  • 31,198
  • 5
  • 66
  • 97
3

To have your array count declared correctly move its declaration beyond the point were nhad been read:

C99 only solution:

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  int count[n]

More flexible (pre C99 also) solution:

#if __STDC_VERSION__ >= 199901L /* going C99 */
# define NEW_ARRAY(t,a,n) t a[n]
# define DELETE_ARRAY(a)
#else
# define NEW_ARRAY(t,a,n) t * a; \ a = malloc((n) * sizeof(t)))
# define DELETE_ARRAY(a) free(a)
#endif

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  {
    NEW_ARRAY(int, count, n);

    ...

    DELETE_ARRAY(count);
  }

  ...
alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    If you are declaring `int count[n];` you are relying on a C99 feature and if your compiler accepts C99, then you do not need to open a block. You can declare a variable anywhere. See http://stackoverflow.com/a/11674997/139746 – Pascal Cuoq Oct 30 '12 at 16:47
  • Yes, you are right. The fresh block I added just for the beauty of it ... :-) @PascalCuoq – alk Oct 30 '12 at 16:51
  • To put some 'rational' meaning behind the new block, I modded my answer to use VLA if going for a C99 build, otherwise allocate the array the classical way. @PascalCuoq – alk Oct 30 '12 at 16:58
  • I cannot upvote you more, but I am definitely going to put these macros in my clip file. – Pascal Cuoq Oct 30 '12 at 17:19
  • 1
    This resolved the issue:#include int main () { int i, n; printf("Enter the length of array: "); scanf("%d", &n); printf ("total of array is %4d \n", n); int count[n]; – devnp Oct 30 '12 at 17:22
  • I know. ;-) @user1504633 – alk Oct 30 '12 at 17:24
  • I do not recommend such two macros for production code, as (for non C99 builds) the call to `malloc()` is missing error checking ... @PascalCuoq – alk Oct 30 '12 at 17:29