-3

I would like to improve my program with user-input of array length. Is that possible in C?

This my code:

int main() {
    int sum = 0;
    int a;
    int array[a] = {};
    printf("Insert length of array:\n");
    scanf("%d", &a);
    for (int i = 0; i < a; i++) {
        printf("Insert number %d \n", i + 1);
        scanf("%d", &array[i]);
    }
    for (int i = 0; i < a; i++) {
        sum = sum + array[i];
    }
    printf("Sum is %d \n", sum);
    return 0;
}

However when I try to compile it, it says: error: variable-sized object may not be initialized

Any possible solution?

chqrlie
  • 131,814
  • 10
  • 121
  • 189

2 Answers2

1

Yeah, in C you couldn't create variable-sized array, and variable-sized in this context means that you can't create array when it's size isn't constant on the compile time. But you can use pointers, and after user input you can allocate array with the appropriate length.

For example:

int* array;
int length;
printf("Enter length of the array: ");
scanf("%d", &length);
// maybe you need to add checking, like if length > 0
array = malloc(sizeof(int) * length);
// now you have array with `length` elements and 0..`length`-1 indexes
alexzok
  • 81
  • 7
0

There are multiple reasons for your program to fail:

  • the definition int array[a] = {}; uses an uninitialized variable a, so the program has undefined behavior. You must define this array after reading the value of a.

  • variable sized automatic arrays cannot have an initializer. You must initialize the array some other way, or not at all since you read the values in the following loop.

  • if the size a is negative or too large, the creation of the local array will have undefined behavior. Allocating from the heap with malloc() or calloc() is preferable for potentially large objects.

  • you do not test the return value of scanf(), causing undefined behavior on invalid input.

Here is a corrected version:

#include <stdio.h>

int main() {
    int sum;
    int a;

    printf("Insert length of array:\n");
    if (scanf("%d", &a) != 1 || a <= 0 || a > 1000) {
        fprintf(stderr, "Invalid input\n");
        return 1;
    }
    int array[a];
    for (int i = 0; i < a; i++) {
        printf("Insert number %d:\n", i + 1);
        if (scanf("%d", &array[i]) != 1) {
            fprintf(stderr, "Invalid input\n");
            return 1;
        }
    }
    sum = 0;
    for (int i = 0; i < a; i++) {
        sum = sum + array[i];
    }
    printf("Sum is %d\n", sum);
    return 0;
}

If you want to handle larger arrays, you can allocate the array with malloc(), but you can also just compute the sum on the fly and not store the values read in an array:

#include <stdio.h>

int main() {
    int a, value, sum;

    printf("Insert length of array:\n");
    if (scanf("%d", &a) != 1 || a < 0) {
        fprintf(stderr, "Invalid input\n");
        return 1;
    }
    sum = 0;
    for (int i = 0; i < a; i++) {
        printf("Insert number %d:\n", i + 1);
        if (scanf("%d", &value) != 1) {
            fprintf(stderr, "Invalid input\n");
            return 1;
        }
        sum += value;
    }
    printf("Sum is %d\n", sum);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189