-2

Can someone explain to me why this code works:

#include <stdio.h>

void set_array(int array[3]);

int main()
{
    int a[3] = {1, 2, 3};

    set_array(a);

    for (int i = 0; i < 4; i++)
    {
        printf("%d\n", a[i]);
    }
}

void set_array(int array[3])
{
    array[3] = 4;
}

How is it possible that I can add an element to an array through a function call? Can someone explain to me what's happening behind the curtains here?

Thanks in advance.

Andrés
  • 51
  • 1
  • 5

1 Answers1

0

You can't, you need to allocate the array using malloc() and then use realloc().

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97