Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
write a c program such that

1)i want to add the elements in an array dynamically till required
2)i don't wanna scan how many elements i need in the beginning
3)i should be able to stop adding the elements whenever i want and i should be able to display it
4)i should be still able to add the elements even though if i displayed it already


What I have tried:

i already tried in such a way that i will be able to scan the number of elements required in the beginning
but i don't wanna scan it in the beginning. i should be able to add the elements till required
Posted
Updated 27-Jun-19 0:47am
Comments
F-ES Sitecore 27-Jun-19 4:02am    
Arrays are fixed so you have to either create a new array each time, or use a linked list and maybe convert the linked list to an array when you have finished adding elements.
PAVAN KUMAR 27-Jun-19 4:04am    
arrays are fixed.
but isn't it possible if we use dynamic allocation.

 
Share this answer
 
Ofcourse can you do it in C and yourself, when using the realloc function which enhance the allocated memory block. If it is a homework than ask your teacher if you are allowed to use some github code.

But be aware that you better understand the memory allocation model of C.

Dont believe anybody that "you cant do that in C". Never, ever believe such filthy lies ;-)
 
Share this answer
 
Comments
Stefan_Lang 27-Jun-19 10:49am    
You're right - I was about to suggest something else and totally forgot about realloc(). It's a perfect fit.
KarstenK 29-Jun-19 13:34pm    
some malloc with bigger size would also be possible, but realloc avoides to copy the existing data in the buffer. ;-)
In C, there is no such thing as an "expandable array" - it is the size it starts off at through the whole life of the application.

There are two ways to do what you want, both relatively complicated in C.
1) Create your own array and provide methods to return the start, the number of elements used, and to add an element. Start off with N elements, and Add items until you reach that. Then create an array double the current size and copy all existing elements into it. Then add the new element, and discard the original array.
2) Don't use an array, use a linked list. This allows you to add new elements as you need them, but does require you to be proficient with pointers.
 
Share this answer
 
Comments
PAVAN KUMAR 27-Jun-19 4:09am    
i know how to do with the linked list.
i just got a doubt that why i cant do with arrays.
thanks :)
OriginalGriff 27-Jun-19 4:24am    
You're welcome!
Basically what Griff said above. use realloc(...) - with capacity doubling - when the array fills up. You will also need to store the used size of the array. I would put that at the start of the array. The github solution above is probably over-engineered for your case, if you do not need inserting and deleting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900