 Hello friends. I am Sanjay Gupta. In this video, I am going to demonstrate you how you can add and elements through dynamic memory location using malloc, calloc and free functions. Before starting, you can note my information. You can connect with me by following these details which are available in front of you. You can search my YouTube channel by typing my name Sanjay Gupta in YouTube search. My channel will be available there. You can open it. Here you will be finding various programming related playlists. So you can improve your programming skills by watching the videos which are available in different playlists. You can click on the subscribe button if you want to follow my YouTube channel. Now I am going to implement solution of this problem. So first I am including a header file std io.h, second header file std lib.h. Now inside main function, I have to declare a pointer, then variable n, variable i and variable s, s is initialized with 0. Then with the help of printf, I am going to display the message enter size. So this size will be of memory block size. That how many blocks you want to create. So this size will be provided by user and that will be received through scanf and stored inside n variable. Now for memory location, I have to call malloc first and this I have to write. So this is dynamic memory allocation. So here malloc function is called here argument n into size of int is passed. So n blocks of size of int will be created and base address will be returned into p. That will be typecasted into integer pointer with the help of this return type of malloc. After this memory location, I have to receive elements from user. So for that purpose, I am typing this message enter elements. So this message will be displayed on console. Now I am implementing for loop which will repeat n times so that n elements can be received from user. And I am writing scanf statement with p plus i. So p is containing base address and I will be added into base address each time so that new memory block addresses will be available and the entered value will be stored on those addresses. So after reading those elements, I have to apply another follow so that I can add the elements. So this loop will be calculating some through this statement s equals to s plus asterisk p plus i. So p plus i will be providing different addresses. Then asterisk will be dereferencing those locations so that values can be identified and those values will be added into s. Now I can print the sum on console with the help of this printf statement. So this way I have implemented the complete code. At last I can call free function so that the allocated memory to p will be derelocated. So it derelocates dynamically allocated memory and then I am writing return 0. So this is the complete code. Now I am going to execute this code. It is asking for size. I am entering 7. So now I have to enter 7 elements. So I am entering one 7 times. You can see the output sum equals to 7. So this way the program is working properly. It is dependent upon user because before entering elements, I have to provide the size that how many elements I want to enter. So this is done with the help of malloc and free. Now I can use caloc function here. So caloc receives two arguments. That's why I have used comma sign here. Rest of the code will remain same. So again I am executing this code. I am entering size as 7 and I am entering 7 elements. So you can see same output is displayed again. So you can either use malloc or you can use caloc whichever is required. But caloc automatically fills 0 in each block which is allocated through it and malloc fills all blocks with garbage values. Now if you want to reduce these two loops into a single loop, then you can do this. You can remove this for loop. And now you can place this statement inside the loop. So just after reading the element through P plus I, it will be added into S. So this way I have converted two loops into a single loop. Again, I am executing this code. I am entering size as 7 and I am entering 7. Sorry, I am entering one 7 times. You can see it is showing same output that is some equals to 7. So this way I have demonstrated you how you can add an element which is entered by user. So it is working as dynamic concept. So you can say that you are creating an array dynamically with the help of caloc and malloc. And after completion of process, you are releasing the allocated memory with the help of free function. So this is all about caloc, malloc and free functions available for dynamic memory location. If you want to watch more programming related videos, you can follow or subscribe my YouTube channel through the url youtube.com slash sanjaygupta underscore tech school. You can download my programming app tech image which is available on Google Play. Thank you for watching this video.