Write a C Program to understand the use of realloc() function

By | March 20, 2017

C Program to understand the use of realloc() function


Write a C Program to understand the use of realloc() function. Here’s a Simple Program to understand the use of realloc( ) function in C Programming Language.


C realloc()


If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc().

Description

The C library function void *realloc(void *ptr, size_t size) attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.

Syntax of realloc() : :

ptr = realloc(ptr, size);


Parameters


  • ptr — This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function.
  • size — This is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.

Return Value


This function returns a pointer to the newly allocated memory, or NULL if the request fails.


Below is the source code for C Program to understand the use of realloc() function which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :



OUTPUT ::



If you found any error or any queries related to the above program or any questions , doubts or reviews , you wanna to ask or share to us, you may Contact Us  through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in the short interval of time.


Thanks for reading the post.

Leave a Reply

Your email address will not be published. Required fields are marked *