In C, when you declare an array, it's size MUST be declared when it is created. There is no mechanism built into C for dynamic array RE-sizing. An array's size is declared explicitly by a statement like int array[12];
or implicitly by initializing it at creation. For example, int array[] = { 31, 25, 16, 5, 2, 2, 8, 21 };
would create an int array with 8 elements and would initialize it with the given values.
Your example with the function is a different situation. The function isn't creating an array, it's using an existing one.
When an array is a parameter given to a function, it isn't creating a new array, it's actually passing an address of an existing array. So, while the address of the array is passed by copy (the address of the array is copied to the function as a parameter, while the original array variable that contains the array address will not be touched by the function), the function will actually be working with the original array contents.
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
int x
then the compiler preserve a space in memory for this datatype so when you declare unknown space variable likeint arr[]
then compiler is unable to allocate memory Herevoid function(int arr[])
i think you are passing a predefined array which the compiler knows it's space