#Creating arrays
11 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Also am I allowed to set the size of an array based on a variable like int array[x]? I saw that someone just used malloc when they needed to set a dynamic size
It's best practice to malloc and free arrays of variables size. And no, int arr[]; should be replaced with int *arr
More compiker friendly
Wdym replaced? How is that used?
An undefined array is problematic with most compilers. A pointer in the other hand can be un initialized pointer in the other hand will not cause problems unless you forget to point it at something later
A pointer and an array are the same semantically
assuming you meant declaring such array outside of a function / struct declarations - you can, such array will be an incomplete type and must be supplied a size later. see https://en.cppreference.com/w/c/language/type#Incomplete_types & https://en.cppreference.com/w/c/language/array#Arrays_of_unknown_size
Also am I allowed to set the size of an array based on a variable like int array[x]?
those are called VLAs (Variable Length Arrays). some compilers supports them, some don't. they are generally discouraged though useful in some cases (type declarations for allocations mostly). see https://en.cppreference.com/w/c/language/array#Variable-length_arrays
because they are generally discouraged, it might be best to use *alloc & friends for dynamic allocations (or the functionallity of any established allocator really)