I'm writing a program that need to scan numbers into an array, and I know that the number of items on my array will be a multiple of 5. I can't use the realloc function, only malloc, But my program mess the 6th item, and after 10 items just crashes. can you help me find my error here? Thanks!
#include <stdio.h>
#include <stdlib.h>
#define K 5
int main(){
int counter=0;
int enteredNum;
int *p=malloc(K*sizeof(int));
int *pmore=NULL;
printf("Please enter the series : \n");
scanf("%d",&enteredNum);
while(enteredNum!=0){
p[counter]=enteredNum;
if(counter%K==0&&counter!=0){
pmore=malloc(((counter)+K)*sizeof(int));
for(int i=0;i<counter;i++){
pmore[i]=p[i];
//for
free(p);
p=pmore;
pmore=NULL;
}//if
counter++;
scanf("%d",&enteredNum);
}
for(int i=0;i<counter;i++)
printf("%d\t",p[i]);
}
p[counter]after the if and before thecounter++