How can you increase the size of a dynamically allocated array?

/* Allocate space for an array with ten elements of type int. */
int *ptr = malloc(10 * sizeof (int));
if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */

realloc

It is often useful to be able to grow or shrink a block of memory. This can be done using realloc which returns a pointer to a memory region of the specified size, which contains the same data as the old region pointed to by ptr (truncated to the minimum of the old and new sizes). If realloc is unable to resize the memory region in-place, it allocates new storage, copies the required data, and frees the old pointer. If this allocation fails, realloc maintains the original pointer unaltered, and returns the null pointer value. The newly allocated region of memory is uninitialized (its contents are not predictable). The function prototype is

void *realloc(void *pointer, size_t size);

Improve Answer Discuss the question "How can you increase the size of a dynamically allocated array?" Watch Question

First answer by ID3411788082. Last edit by Neha 0782. Contributor trust: 8 [recommend contributor]. Question popularity: 17 [recommend question]

Research your answer:

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > How can you increase the size of a dynamically allocated array?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
Strings and Arrays?  Size of an array in c?  How do you dynamically create an array?  How do you increase size of dynamically?  Increase size of dynamically allocated array?  Increase the size of a dynamically allocated array?  Maximum size that can be allocated by a malloc call?  Can you dynamically allocate arrays in expanded memory?  How do you increase size of dynamically allocated array?  How can you increase th size of dynamically allocated array?  How can you increase the size of dynamically allocated array?  Ow can you increase the size of a dynamically allocated array?  How can you increase the size of a dynamicaly allocated array?  How can you dynamically allocate a multidimensional array in c?  How can you increase the size of an dynamically allocated array?  28 How can you increase the size of a statically allocated array?  12 How can you increase the size of a dynamically allocated array?  26 How can you increase the size of a dynamically allocated array?  52 How can you increase the size of a dynamically allocated array?  68 How can you increase the size of a dynamically allocated array?