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.
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.
Videos
I'm new to C and I'm trying to iterate over an array and when all values up to max size are assigned I have no problem but when I leave any values empty and do the for loop it takes some junk values from memory. I was wondering if there is some "end value" like \0 for string to know where to stop.
Sorry for the bad english and thank you in advance
It's valid and allowed since C99. It's called flexible array member - useful to make the last member of the struct variable-length and it must be the last member of the struct if used.
You are looking at a C99 feature, Flexible array member. If the array is defined at the end of the struct, then you will define its size during allocation via malloc, by allocating a bigger chunk of memory.
for an array of size len:
struct st *mySt = malloc(sizeof(struct st) + len * sizeof(mySt->arr[0]));
Is it possible to assign a blank space as an element in an array if yes how?
Initialize the results array so all elements are NULL:
char* results[10] = { NULL };
In the posted code the elements are unitialized and will be holding random values.
Additionally, prevent going beyond the bounds of the results array:
while (i < 10 && result)
{
}
There's no such thing as an "empty array" or an "empty element" in C. The array always holds a fixed pre-determined number of elements and each element always holds some value.
The only way to introduce the concept of an "empty" element is to implement it yourself. You have to decide which element value will be reserved to be used as "empty value". Then you'll have to initialize your array elements with this value. Later you will compare the elements against that "empty" value to see whether they are... well, empty.
In your case the array in question consist of pointers. In this case selecting the null pointer value as the reserved value designating an "empty" element is an obvious good choice. Declare your results array as
char * results[10] = { 0 }; // or `= { NULL };`
an later check the elements as
if (results[i] == NULL) // or `if (!results[i])`
/* Empty element */
If an array is uninitialized, then there is no way that your program can determine that just based on the array. You would need to "remember" that information yourself, e.g.:
#include <stdbool.h>
int main (void)
{
int a[3] ;
bool a_empty = true;
// ...
if ( a_empty )
printf("a is empty");
// ...
a[0] = 1;
a_empty = false;
}
Another approach would be to define a particular set of contents as representing "empty" (this is called sentinel values):
#include <stdbool.h>
enum { EMPTY = -1 };
bool is_empty(int a[3])
{
return a[0] == EMPTY && a[1] == a[0] && a[2] == a[0];
}
int main (void)
{
int a[3] = { EMPTY, EMPTY, EMPTY };
// ...
if ( is_empty(a) )
printf("a is empty");
}
Of course you would have to choose sentinels that are not valid data for the expected array contents. If no such sentinels exist then you have no choice but to use the extra variable as in my first example.
You might be able to make an empty array and then compare your array too that, if it is the same then the array is empty, else it must have something in it.
void isEmpty(int *a)
{
int compare[3]
if ( a == compare[2])
printf("is empty!\n") ;
else
printf("not empty!\n");
}
int main (void)
{
int a[3] ;
isEmpty(a) ;
a[0] = 1 ;
isEmpty(a) ;
return 0 ;
}
I haven't had a chance to test this, but have done something similar in PHP before.
*array == NULL is wrong. You are dereferencing the pointer first (which could lead to a segfault if the pointer really is null) and then comparing its int value to a pointer value. Moreover, your compiler will perfectly accept that erroneous expression if NULL is defined as just 0 and not (void *) 0.
You should be checking array == NULL to see if the passed pointer refers to anything, and then dereference it only in case it's not NULL.
Be aware, however, that dereferencing a non-null pointer isn't guaranteed to be a safe operation either. If the pointer contains a garbage value because it was allocated on the stack and not initialized, or if it refers to a deallocated region of memory, nasty bugs can happen.
You want if (array == NULL) -- but unless you first initialize array to NULL, it won't do any good either. I think you'd be better off backing up and telling us a bit more about what you're trying to accomplish, and trying to get help trying to accomplish your overall goal.