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.
Best way to create an empty collection (array and list) in C# (.NET)
Is there an "Empty List" singleton in C#? - Stack Overflow
Any good reason to allow for empty linked list?
empty array in C and returning a pointer - Stack Overflow
Hello, I've been making a linked list, and talking about it with my dad too, he insists that you should be allowed to make an empty linked list, but I don't think there should be, since there's "no reason to store nothing."
Thoughts?
Edit: feel free to continue posting your thoughts, but after some thought, I'll reconsider allowing an empty list, since having the end user work around this issue would probably make it overall more work. Thank you very much for your input though! I'll let my dad know most of you agree with him 😂
Edit 2: alrighty, I've thought about it, I'll definitely be implementing the support for an empty linked list (though I'll have to rewrite a large chunk of code, rip lol) I definitely enjoyed talking with you guys, and I look forward to finally posting my implementation.
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
In C, linked lists are usually implemented as a series of nodes stored on the heap that point to eachother. The heap is a persistent memory area that runs throughout the life-cycle of the program.
When you create a variable normally in a C function, and the function returns, the variable that you created is no longer accessible. However when you create something on the heap in a function, and the function is returned, the data you allocated on the heap is still there. However, you have no way of accessing it-- unless the function returns a pointer.
So what you would do for create_q() would be to create the linked list on the heap (using a function in stdlib.h called "malloc"), and then you would return a pointer to your first node, letting the main function know where on the heap to find the first node. Then that first node would have a pointer in it, telling the program where on the heap to find the second node, and so forth.
However, you're probably approaching linked lists the wrong way. Unless this is for some sort of homework project, you probably wouldn't want to create an empty linked list. One of the benefits of a linked list is that it's a dynamic structure in which you can easily insert new nodes. You could still have some variable keeping track of the maximum size you want the list to be, but you probably wouldn't want to actually create the nodes until you had to.
Just keep in mind what a linked list is. It's a set of nodes floating on the heap (in C) that each store some data, and contain a pointer to the next node floating on the heap. All you need, to access the linked list, is a pointer to the first node. To add a new node, you simply "walk" through the list till you reach the last node, and then create a new node and have the old-last node point to it.
Is this what you had in mind?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node_t
{
int value;
int priority;
struct node_t *next;
};
static int current_size;
static int max_size;
static struct node_t* head = NULL;
struct node_t* create_q(int);
struct node_t* create_q(int max_terms)
{
int i; // loop counter/index
current_size = max_terms;
max_size = max_terms;
if( NULL == (head = malloc(sizeof(struct node_t)*max_terms)))
{ // then, malloc failed
perror("malloc failed for struct node_t list");
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// set all fields to '0,0,Null'
memset( head, 0x00, sizeof(struct node_t)*max_terms);
// set all next links, except last link
for(i=0;i<(max_terms-1);i++)
{
head[i].next = &head[i+1];
}
// set last link
head[i].next = NULL;
return( head );
} // end function: create_q
It's really easy:
myList.Clear();
If by "list" you mean a List<T>, then the Clear method is what you want:
List<string> list = ...;
...
list.Clear();
You should get into the habit of searching the MSDN documentation on these things.
Here's how to quickly search for documentation on various bits of that type:
- List Class - provides the
List<T>class itself (this is where you should've started) - List.Clear Method - provides documentation on the method Clear
- List.Count Property - provides documentation on the property Count
All of these Google queries lists a bundle of links, but typically you want the first one that google gives you in each case.