0

Variable length arrays are not allowed. But then, I recently learned that user-defined function manipulates the original array, not its own personal copy. So I thought of creating a function that gets the desired size of the array by the user and therefore modifying the size (I also initialized the array in this function).

void fill_array(int array[], int size, int in_val);

void main(){
    int n, value, list[1], ctr;
    clrscr();

    printf("Enter the size of the array: ");
    scanf("%d", &n);
    printf("Enter the value that you want all of the elements to take initially: ");
    scanf("%d", &value);
    printf("\nAfter scanning value, n = %d\n", n);

    fill_array(list, n, value);

    printf("\nAfter Function fill_array Execution");
    printf("\nThe values of each element of the array is now: %d ", list[0]);
    printf("%d ", list [1]);
    printf("%d ", list [2]);
    printf("\nn = %d\n", n);
    printf("value = %d\n", value);

    getch();
}

void fill_array(int array[], int size, int in_val){
    int ctr;

    for (ctr = 0; ctr < size; ++ctr)
        array[ctr] = in_val;
    printf("\nInside Function");
    printf("\nn = %d\n", size);
    printf("value = %d\n", in_val);
}

Here's the console / a sample run:

Enter the size of the array: 3

Enter the value that you want all of the elements to take initially: 444

After scanning value, n = 3

Inside Function

n = 3

value = 444

After Function fill_array Execution

The value of each element of the array is now: 444 444 444

n = 444

value = 444

The function did modify list. However, as you can see, it also changed the value of n. After every fill_array execution, n always equals value. Will someone please explain to me why the function is changing the value of n. I don't want the value of n to change.

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
  • 3
    You declared `list[1]`. That's an array with size 1. You cannot modify the size. What you have is undefined behavior which should not be described further. – DeiDei Sep 21 '18 at 06:36
  • Yes, DeiDei is right ... declare the array once you know the value of`n` with `int list[n]`. – YesThatIsMyName Sep 21 '18 at 06:39
  • 1
    @YesThatIsMyName and that would be a VLA which was not allowed. – Antti Haapala -- Слава Україні Sep 21 '18 at 07:28
  • 1
    **Don't use TurboC++** (or TurboC) since it is an obsolete compiler for an obsolete dialect of C++ (or of C). If you want to learn C++, learn at least C++11 and use a compliant compiler (e.g. [GCC](http://gcc.gnu.org/) or [Clang](http://clang.llvm.org/), both being [free software](https://en.wikipedia.org/wiki/Free_software)). If you are learning C, learn at least C99 and preferably C11 (and both GCC and Clang support it) – Basile Starynkevitch Sep 21 '18 at 07:35
  • @AnttiHaapala True, I wrote that as an answer. – YesThatIsMyName Sep 21 '18 at 07:54
  • Your `list` array is large enough to hold one element. You can't change its size. The only valid index into the array is `0`. Anything else will index outside of the array and result in undefined behavior. It doesn't matter whether the access happens in `main` or in a function that `main` calls (passing `list` as an argument). – Tom Karzes Sep 21 '18 at 07:58
  • Possible duplicate of [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Tom Karzes Sep 21 '18 at 07:59
  • @TomKarzes This is not a duplicate of his question, but describes only why his output is as it is. – YesThatIsMyName Sep 21 '18 at 08:09

3 Answers3

1

Within the fill_array function you are trying to write into memory that is out of bounds for list array. Doing so will result in undefined behaviour.

From wiki article on undefined behaviour:

The behavior of some programming languages—most famously C and C++—is undefined in some cases. In the standards for these languages the semantics of certain operations is described as undefined. These cases typically represent unambiguous bugs in the code, for example indexing an array outside of its bounds.

The C Committee draft (N1570) states this about Undefined Behavior:

3.4.3
1 undefined behaviour
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

P.W
  • 26,289
  • 6
  • 39
  • 76
1

Once an array is defined, there is no way to change its size.

A workaround for this is to use dynamic memory allocation and then use the allocated memory as if it is an array. This could look something like:

printf("Enter the size of the array: ");
if (scanf("%d", &n) != 1) exit(1);
printf("Enter the value that you want all of the elements to take initially: ");
if (scanf("%d", &value) != 1) exit(1);

int * list = malloc(n * sizeof *list); // allocate memory

fill_array(list, n, value);

// and just use it as an array
list[1] = list[0] + 42;         // assuming n >= 2
...
...

free(list);

If you later on need to resize the array, you can use the function realloc

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

As the requirement is that "Variable lenght arrays are not allowed" the only possible soulution is to allocate an array of "big enough" size.

Then you do not pass as n the size of the array, but the number of elements.

So you just loop over the array and stop at lenght n. You need to rewrite the function fill_array to accomplish this behavior.

Why you get the output you get is because of undefined behaviour. See P.W's answer for more info on that.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30