I am trying to make an empty array. Here is my code:
if(n == 0 || n == 1)
{
factors[] = {};
}
However, it says that it expects an expression inside of [] and {}. What is the correct syntax to do such a thing?
TL;DR You cannot because the requirement is invalid from C point of view. Array sizes are fixed and cannot grow or shrink whatever be the usage requirement.
Quoting C11, chapter §6.7.6.2, Array declarators (emphasis mine)
In addition to optional type qualifiers and the keyword static, the
[and]may delimit an expression or*. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. [...]
Also, as per the Initialization syntax, C11, chapter §6.7.9, the brace closed initializer list should have at minimum one initializer element (object). An initializer in form of { }; is invalid C.
Note:
If you meant "How do I make the contents of an array empty in C?", well, in that case, assuming "empty" translates to a value of 0, we can use memset() or a loop-and-assignment to get that done. This, however, makes the array contents empty, not the size of the array.
Try executing these two cases, so that you can understand what's actually going on with gcc.
CASE 1:
#include<stdio.h>
int main()
{
int arr[]={};
printf("size:%d\n",sizeof(arr));
}
CASE 2:
#include<stdio.h>
int main()
{
int arr[]={};
arr[0]=100;
printf("size:%d\n%d\n",sizeof(arr),arr[0]);
}
This is because name of an array represents the address of base element. Also, if you declare an array with particular size, you cannot define the boundaries and you can access memory even out of boundaries till the segment in which the array is declared exhausts out of memory.
Coming to your code factors[] = {};, you cannot do this because this is not declaration of the array factors, though you are trying to assign no values using the {} construct. You can leave the [] empty only when you are initializing the array.
int arr[]={}; is invalid (only in theory). But practically, this is valid and possible. If you have executed the two cases I've mentioned in the answer, you will understand this clearly.
Apr 19, 2017 at 6:29
gcc -std=c11 -pedantic-errors, where -std=c11 is the current C standard and pedantic-errors means "actually compile according to the requirements of the standard". If you do not set these, you are not using GCC as a standard C compiler, period.
Technically you can't make an array empty. An array will have a fixed size that you can not change.
If you want to reset the values in the array, either copy from another array with default values, or loop over the array and reset each value.
You can't free the memory of array, because it is fixed. you can set some default values like '0' using memset function.
void *memset(void *arrptr, int value, size_t n);
The expressions [] & {} can be used at the time of declaration of array
my instructor said Should you be given the input 0 or 1, return an empty list. Notice thatlistandarrayare typically different things.listnormally refers to some sort oflinked list. A list can be empty (akahead == NULL). An array can not. So maybe you misunderstood your assignment by usingarray.