-4
#include <stdio.h>

void copyArrInt(int arr1[], int arr2[], int arrSize) {
    for (int i = 0; i < arrSize; i++) {
        arr2[i] = arr1[i];
    }
}

int main(void) {
    int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int arr2[] = {};
    // int arr2[10] = {} works as expected though

    copyArrInt(arr1, arr2, 5);

    for (int i = 0; i < 10; i++) {
        printf("%d ", arr2[i]");
    }

    return 0;
}

I expected the output without specifying the arr2 size to be 10 elements, but only with the first 5 equal to the first array's.

I want to know why it is happening, since I know that initializing the array with its size set to 10 will give me the expected output.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 5
    Look up "undefined behavior". – n. m. could be an AI Aug 13 '23 at 16:50
  • 2
    `int arr2[] = {};` is a syntax error. – Weather Vane Aug 13 '23 at 16:52
  • @n.m.couldbeanAI Thanks, that's exactly what I was looking for!! – NicolasPaiva Aug 13 '23 at 16:53
  • 4
    With this statement: `int arr2[] = {};` you are effectively saying that the length of the array is 0. This is just plain list initialization. Remember, arrays are not dynamically resizable in C. – Eldinur the Kolibri Aug 13 '23 at 16:53
  • @EldinurtheKolibri this appears to be (and is tagged) "C" not "C++". But the rest of your comment is still right. – David Gelhar Aug 13 '23 at 16:54
  • @DavidGelhar oups :) – Eldinur the Kolibri Aug 13 '23 at 16:55
  • @WeatherVane Is that a syntax error? The program compiled fine, but the output was not what I expected... I am a beginner, but as far as I am concerned, I don't need to specify the size of the array – NicolasPaiva Aug 13 '23 at 16:57
  • MS VC says `error C2059: syntax error: '}'` – Weather Vane Aug 13 '23 at 16:58
  • @WeatherVane Yes, the " was meant to be there. Even though that was not the actual problem, thank you very much :) – NicolasPaiva Aug 13 '23 at 17:01
  • @WeatherVane Did you actually look at the code or just copy paste? Of course there is a syntax error. They guy literally put a quotation mark in the middle of nowhere. – Eldinur the Kolibri Aug 13 '23 at 17:01
  • @EldinurtheKolibri the syntax error is in `int arr2[] = {};` and there is no quotation mark there. I copied that line into a simple `main()` function I already have. However, a small change to `int arr2[] = {0};` allowed it to compile. That is an array with 1 element, which will be undefined behaviour at run time in the OP's code. – Weather Vane Aug 13 '23 at 17:02
  • @WeatherVane On gcc that compiles just fine. Only after enabling `-Wpedantic`, I get an error. This is still not a syntax error but a compilation error. So I disagree with your statement. – Eldinur the Kolibri Aug 13 '23 at 17:09
  • 1
    @EldinurtheKolibri Empty initializer lists are not valid C syntax before C23. The fact that GCC allows them as an extension doesn't change that. Since C23 they are allowed, but 0-sized arrays are not so the code is still not valid C. – interjay Aug 13 '23 at 17:12
  • @interjay Oh ok. I might be wrong then. But for me it still compiles, even with `-std=c11`, `-std=c89`. – Eldinur the Kolibri Aug 13 '23 at 17:15
  • @EldinurtheKolibri did you actually read my comments? – Weather Vane Aug 13 '23 at 17:21
  • 1
    @WeatherVane Yes. But I was obviously wrong: https://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c :). Sorry – Eldinur the Kolibri Aug 13 '23 at 17:22
  • @NicolasPaiva: Re “Yes, the " was meant to be there.”: The code will not compile with the errant `"` in `printf("%d ", arr2[i]");`. Yet your post asks about output when the program does compile. It does not make sense that you mean the `"` to be there. Perhaps your comment was mistyped? If so, edit the question to remove the `"` from the code because it is not part of the actual question. If not, explain what you mean by the `"` was intended to be there. Why was it intended to be there? – Eric Postpischil Aug 13 '23 at 17:24
  • @NicolasPaiva: I wasn't referring to the stray `"` in an [earlier comment](https://stackoverflow.com/questions/76894349/why-is-this-function-copying-all-the-elements-of-the-array#comment135556398_76894349) (I hadn't noticed it) but to `arr2[]`'s initialisation. – Weather Vane Aug 13 '23 at 17:50

1 Answers1

1

arrays are not dynamically sized objects in c, they are created with a specifci size and their size does not change.

THe size is specified either explicitly

 int arr[10];

or implicitly by specifying the values

 int arr[] = {1,2,3};

(or both, like you did iin your first array)

 int arr[3] = {1,2,3};

But

  int arr[] = {};

gives no size clues, so even if it compiles you end up with a zero size array

Then combine this with the fact that C has no idea of array bound checking, makes you array copy function do undefined things by trying to write to a zero size array

pm100
  • 48,078
  • 23
  • 82
  • 145