Search code examples
arrayscnetbeansintegercygwin

RUN FAILED (exit value 1, total time: 2s)


I'm trying to store a large digit number (10^6) into an array. I'm first trying to get input as a string then store it in an integer array. Here is the code :

#include<stdio.h>
#include<string.h>
#define MAX 1000000
int main()
{
    char ip[MAX];
    int num[MAX];
    int ch,i=0;
    
    while((ch = getchar()) !='\n') {
        ip[i++] = ch;
    }
    for(i = 0;i<=strlen(ip);i++) {
        num[i] = ip[i]-48;
    }
    
    return 0;
}

This is working fine when I am using a small number for MACRO MAX, for example 100. But I am getting an error with a large number. I am using netbeans. I tried this in vscode too but their I am getting Cygwin stack dump error.


Solution

  • strlen(), expects a NULL terminated array of characters, meaning that you are missing a \0 after your while loop.

    This is also prone to overflow since you are not checking if your i value is smaller than MAX - 1.

    After your while loop:

    ip[i] = 0

    or when declaring

    char ip[MAX] = {0}