-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
42 lines (33 loc) · 913 Bytes
/
Copy pathrealloc.c
File metadata and controls
42 lines (33 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int n, i;
printf("Enter total number of values: ");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("\nEnter the values:\n");
for (i = 0; i < n; i++) {
scanf("%d", (ptr + i));
}
printf("\nEnter updated size of n: ");
scanf("%d", &n);
int *temp = (int*)realloc(ptr, n * sizeof(int));
if (temp != NULL) {
ptr = temp;
} else {
printf("Memory reallocation failed! Using old memory.\n");
}
printf("\nPrevious address: %p, New address: %p\n", (void*)ptr, (void*)temp);
printf("\nThe entered values are:\n");
for (i = 0; i < n; i++) {
printf("%d\t", *(ptr + i));
}
free(ptr);
return 0;
}