Hi
I am looking to implement an Array where the size doesn't have to be stated.
At the moment i have:
So the size is 8 but how can I code it so the array size isn't defined?Code:int num [8] = {76, 13, 14, 1, 55, 33, 24, 65}
Thanks
Tom
Hi
I am looking to implement an Array where the size doesn't have to be stated.
At the moment i have:
So the size is 8 but how can I code it so the array size isn't defined?Code:int num [8] = {76, 13, 14, 1, 55, 33, 24, 65}
Thanks
Tom
you meant like this?
you don't need the size of the array when you initialize it. however, you'd need it later when you want to operate on the array. otherwise, you'll have to use malloc to allocate the memory, and implement something like the linked-list.Code:int num [] = {76, 13, 14, 1, 55, 33, 24, 65}
"All that we see or seem
Is but a dream within a dream." - Poe
Hi
Thanks for the reply. I have seen the maloc function when searching google but have no idea how to use it.
to use malloc with dynamic array, you still need the size of the array.
Code:int *array = malloc( 8 * sizeof(int)); <-- allocated space for 8 intergers. free(array); // call free after done with it.
Last edited by nimitzhunter; 02-13-2011 at 04:36 PM.
"All that we see or seem
Is but a dream within a dream." - Poe
Ok thanks for the reply.
So if you have to define the size of maloc array then how can you have undefined array size?
I am new to C and still learning thanks.
You allocate what you need with malloc(). Expand or shrink with realloc().So if you have to define the size of maloc array then how can you have undefined array size?
Release the memory with free().
That has worked thanks.