+ 1

please help to find my mistek

here is a simple code of c language, it is about malloc,calloc and realloc. the last two for loop are not working properly, I am not getting any error massage from compiler please help me to find my mistek here is a code: #include<stdio.h> #include<stdlib.h> void main() { int *m,*o,n,i,p,v; m=(int*)malloc(sizeof(int)*n); printf("how many number you wanna add(malloc):\n"); scanf("%d",&n); printf("enter the number:\n"); for(i=0;i<n;i++) { scanf("%d",&m[i]); } for(i=0;i<n;i++) { printf("%d",m[i]); } free(m); printf("\n_______________\n\n"); o=(int*)calloc(v,sizeof(int)); printf("how many number you wanna add(calloc):\n"); scanf("%d",&v); printf("enter the number:\n"); for(i=0;i<v;i++) { scanf("%d",&o[i]); } for(i=0;i<v;i++) { printf("%d",o[i]); } free(o); printf("\nenter the new size of malloc:\n"); scanf("%d",&p); m=(int*)realloc(m,sizeof(int)*p); if(p>n) { printf("add the numbers in malloc:\n"); for(i=0;i<p;i++) { scanf("%d",&m[i]); } } for(i=0;i<p;i++) { printf("%d",m[i]); } free(o); }

12th Sep 2021, 4:49 PM
Vivek Kandoliya
Vivek Kandoliya - avatar
1 Answer
0
You should write this statements after giving the variables. (after scanf). Cause initial values of them are 0 so it may be a problem. m=(int*)malloc(sizeof(int)*n) o=(int*)calloc(v,sizeof(int)) I hope it works. Happy coding! https://code.sololearn.com/c8Xx74zXjW0j/?ref=app
12th Sep 2021, 5:03 PM
mesarthim
mesarthim - avatar