简体   繁体   中英

How to initialise an variable sized int array to 0 in C?

I was trying out the insertion of elements program in C. There I need to initialise a VARIABLE SIZED ARRAY TO 0 but the compiler is not allowing me to do so. Any way out?

#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 (y/n): ");
        scanf(" %c",&c);
    }
    c='y';
    while(c=='y'||c=='Y') {
        printf("\n Enter the element to be inserted: ");
        scanf("%d",&k);
        printf("\n Enter the position: ");
        scanf("%d",&pos);
        pos--;
        insert(arr,k,pos,n);

        printf("\n Continue (y/n): ");
        scanf(" %c",&c);
        if(c=='n'||c=='N')
            printf("\n !!Thank You!!");

    }

    return 0;
}

when I tried out

int arr[n]={0};

it displayed an error that could not initialise a variable sized array to 0.

output of prog

Enter the position: 2

Displaying array elements: 2 5 3 -344174192
Displaying array elements: 2 4 5 3
Continue (y/n):

...Program finished with exit code 0
Press ENTER to exit console.

garbage value is shown in bold.

Use memset to clear your array:

memset(arr, 0x00, sizeof(arr));

When using sizeof with VLA, result of sizeof is not evaluated in compile time, so it is perfectly to use.

Edit: While your current approach works, I would strongly advice you to move away from VLA and in case you need VLA, use malloc and free functions. This will significantly decrease chances for stack overflow, in case of n being big number`.

#include "stdlib.h"

int main() {
    int* arr;
    int n;

    //Get n value by input

    arr = malloc(sizeof(*arr) * n);
    if (arr == NULL) {
        //No memory available, stop program
    }
    memset(arr, 0x00, sizeof(*arr) * n);

    //Do you job here just like before using VLA

    //At the end, before returning, call free function
    free(arr);
}

FYI, VLA stands for Variable Length Array .

So, I finally the code snippet is as follows

#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];
memset(arr, 0x00, sizeof(arr[0]) * n);
printf("\n Enter the array elements: ");
while(c=='y'||c=='Y')
{   
    scanf("%d",&arr[i]);
    i++;
    printf("\n Continue (y/n): ");
    scanf(" %c",&c);

}
c='y';
while(c=='y'||c=='Y')
{
    printf("\n Enter the element to be inserted: ");
    scanf("%d",&k);
    printf("\n Enter the position: ");
    scanf("%d",&pos);
    pos--;
    insert(arr,k,pos,n);

    printf("\n Continue (y/n): ");
    scanf(" %c",&c);
    if(c=='n'||c=='N')
        printf("\n !!Thank You!!");

}

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM