realloc () : invalid pointer error
Question;-
Today, I write a program on realloc() in visual studio code. Input;- #include<stdio.h> #include<stdlib.h> void main() { int n,b; int *ptr; int *p,*q; int i,c=0; printf("\nEnter the size of array = "); scanf("%d",&n); ptr = (int*)malloc(n*sizeof(int)); if(ptr==NULL) { printf("Error,its envolved"); exit(0); } p = ptr; printf("\n\nEnter Element in block;-\n\n"); for(i=0;i<n;i++) { scanf("%d",ptr); c = c + *ptr; ptr++; } printf("\nSum = %d\n",c); printf("\nElements in Block;-\n\n"); for(i=0;i<n;i++) { printf("\n%d",*p); p++; } printf("\nNew size of array = "); scanf("%d",&b); ptr = (int*)realloc(ptr,b*sizeof(int)); if(ptr==NULL) { printf("Error,its envolved"); exit(0); } q = ptr; printf("\n\nEnter Element in block;-\n\n"); for(i=0;i<b;i++) { scanf("%d",ptr); c = c + *ptr; ptr++; } printf("\nSum = %d\n",c); printf("\nElements in Block;-\n\n"); for(i=0;i<b;i++) { printf("\n%d",*q); q++; } } output;- realloc(): invalid pointer Aborted (core dumped) {DEAR SIR/MAM, HOW CAN I FIX THIS? } |
first of all you need to use code tags to post your code, that will keep the original formatting. It is now quite hard to read.
|
I had submitted a great answer, but then I found I misunderstood your program.
Yes, please use code tags. |
Quote:
Troubleshooting advice: Find the most recent location in the program where the pointer is set. |
All times are GMT -5. The time now is 12:42 PM. |