#Creating arrays

11 messages · Page 1 of 1 (latest)

keen mountain
#

I know that arrays either need to have a defined sized, int array[7], or you can just give it data and it will determine the size, int array[]= {1,2,3}, but can I just do int array[];?

hasty impBOT
#

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.

keen mountain
#

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

wooden jasper
#

yes?

#

idk about this tho int array[]= {1,2,3}

timber path
keen mountain
timber path
#

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

frank plover
frank plover
# keen mountain Also am I allowed to set the size of an array based on a variable like int array...

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)