首页 > 解决方案 > 将元素插入动态分配的数组

问题描述

此代码应该在数组 a 中插入 3 个元素,该数组 a 已在函数 main 中动态分配

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *a=malloc(3*sizeof(int));
    int i;
    printf("enter the elements of the array");
    for(i=0;i<sizeof(a)/sizeof(int);++i)
    {
        scanf("%d",&a[i]);
    }
    free(a);
    return 0;
}

但它只插入一个元素并终止,而如果我在统计上定义数组 a 它工作得很好

标签: c

解决方案


推荐阅读