My function does not work and I do not know why, it ends after entering the range. Could you explain why and how to fix it? I need to do this using these pointers to the array.
void generate(int *pa, int *pa2);
void display(int *pa, int *pa2);
int main()
{
srand(time(0));
int size;
printf("Enter size of array\n");
scanf("%d",&size);
int *A=(int*) malloc(size*sizeof(int));
int *ptr=A;
int *pa=&ptr[0];
int *pa2=&ptr[size-1];
generate(pa,pa2);
display(pa,pa2);
return 0;
}
void generate(int *pa, int *pa2)
{
int upper,lower;
int randi;
printf("enter range");
scanf("%d %d",&lower,&upper);
for (int i = 0; i <*pa2; i++)
{
randi=(rand() % (upper - lower + 1)) + lower;
*(pa+i) = randi;
}
}
i <*pa2certainly seems wrong.malloc(), you can hide several compiler errors that will make your bug more difficult to find than if you simply don't cast it. Never cast the result of malloc.