The compilation will pass, but an error will be reported.

encountered a strange problem

procedure is simplified like this, first enter an integer n, then enter n integers into the array
and then enter an integer m, and then enter m integers into the array
is required to intersect, as a result, there is a problem in the for loop, as long as the assignment operation appears in the for loop to report an error
for example, I wrote flag=5, but temp = * (bp+i); tempPP can. What"s going on with asking for advice?

-sharpinclude <stdlib.h>
-sharpinclude <stdlib.h>
int main(void){
    int a,b;
    int i,j;
    int flag;
    int *ap = (int *)malloc(sizeof(int)*a);
    int *bp = (int *)malloc(sizeof(int)*b);

    scanf("%d",&a);
    for(i=0;i<a;iPP){
        scanf("%d",ap+i);
    }

    scanf("%d",&b);
    for(i=0;i<b;iPP){
        scanf("%d",bp+i);
    }

    /********************************/

    int *p = (int *)malloc(sizeof(int)*(a+b));
    int *temp = p;
    for(i=0;i<a;iPP){  //a
        *(p+i) = *(ap+i);
    }
    temp = temp + a ; //

    for(i=0;i<b;iPP){  //b

        flag = 5 ;

        /*temp = *(bp+i);
        tempPP;*/
    }

    //printfArr(p,a);
    //printfArr(p,a+b);
    return 0;
}
Mar.05,2021

when you use malloc to apply for array space a code b has not yet been assigned, and their values are undefined. The length of the array you applied for does not know how long it is, so it will cross the line when assigning values to the array.
solution:
put the two scanf statements assigned to a before Malloc .


solved

int ap = (int ) malloc (sizeof (int) * a);
int bp = (int ) malloc (sizeof (int) * b);

)

this is out of bounds

Menu