How to dynamically increase the array size?

前端 未结 3 1232
一向
一向 2021-02-03 12:01

I\'ve been trying to make a program that adds 2 arrays of different size. But I would like to know to to dynamically increase the array size capacity? Ex: array[4] then upgrade

3条回答
  •  自闭症患者
    2021-02-03 12:22

    #include 
    #include 
    int main()
    {
    int *p,*q;
    int i;
    p=(int *)malloc(5*sizeof(int));
    p[0]=3;p[1]=5;p[2]=7;p[3]=9;p[4]=11;
    q=(int *)malloc(10*sizeof(int));
    for(i=0;i<5;i++)
    q[i]=p[i];
    free(p);
    p=q;
    q=NULL;
    for(i=0;i<5;i++)
    printf("%d \n",p[i]);
    return 0;
    }
    

提交回复
热议问题