c realloc
/*prototype of realloc*/
void *realloc(void *ptr, size_t newsize);
/*use*/
dyn_array = realloc(dyn_array, (dyn_array_size + 1) * sizeof(int));
/*
adds one more space to dyn_array
you need stdlib.h for this
*/
c realloc
/*prototype of realloc*/
void *realloc(void *ptr, size_t newsize);
/*use*/
dyn_array = realloc(dyn_array, (dyn_array_size + 1) * sizeof(int));
/*
adds one more space to dyn_array
you need stdlib.h for this
*/
realloc
int ptr_new = *realloc(void *ptr, size_t size);
Where 'ptr_new' is the new pointer you want to point to the required Dynamic
memory, 'ptr' is the already existing pointer to a dynamic memory and 'size'
is the new size of dynamic memory that you want to allocate.
Working:
Realloc creates/allocates a new memory in heap with specified size and copies
all the contents from the previous memory pointer to by the pointer 'ptr'.
It the deallocates the memory in heap pointed to by the old pointer i.e, 'ptr'.
Note: pointer 'ptr' should also point to a memory in heap.
realloc in c
#include <stdio.h>
int main () {
char *ptr;
ptr = (char *) malloc(10);
strcpy(ptr, "Programming");
printf(" %s, Address = %u\n", ptr, ptr);
ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
strcat(ptr, " In 'C'");
printf(" %s, Address = %u\n", ptr, ptr);
free(ptr);
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us