小编ANI*_*DAK的帖子

如何在C中将变量大小的int数组初始化为0?

我正在尝试在C中插入元素程序.我需要将VARIABLE SIZED ARRAY初始化为0,但编译器不允许我这样做.任何出路?

#include <stdio.h>

void insert(int arr[],int k,int pos,int n)
{
    display(arr,n);
    int i=n-1;
    if(pos<n) {
        while(i>=pos) {
            arr[i]=arr[i-1];
            i--;
        }
        arr[pos]=k;
        display(arr,n);
    } else {
        printf("\n !!Array full!!");
        return 0;
    }
}


void display(int arr[],int n)
{   
    int i;
    printf("\n Displaying array elements: ");
    for(i=0;i<n;i++)
        if(arr[i]!=0)
            printf("%d ",arr[i]);
}


int main()
{   
    int n,pos,i=0,k;
    char c='y';
    printf("\n Enter the no. of elements: ");
    scanf("%d",&n);
    int arr[n];
    printf("\n Enter the array elements: ");
    while(c=='y'||c=='Y') {   
        scanf("%d",&arr[i]);
        i++;
        printf("\n Continue …
Run Code Online (Sandbox Code Playgroud)

c arrays

-1
推荐指数
1
解决办法
88
查看次数

标签 统计

arrays ×1

c ×1