Unhandled exception at 0x53918F0E (ucrtbased.dll)

-1
**#include <stdio.h>
#include <string.h>

void buildArray(); 
int main(void)
{
    int example[20];
    buildArray(example);
    getch();
}

void buildArray(int param[]) {
    int i = 0;
    do {
        printf("Please enter number :");
        scanf("%d", param[i]);
        i++;
    } while (param[i] != -1);
}**

hello i want to get input from user. when will user entered -1 number , i want to break this loop. But i getting some error code : ""Unhandled exception at 0x53918F0E (ucrtbased.dll) in HelloC.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.""

where is my fault? When i was try to change function type from void to int, getting same error.

c
function
loops
do-while
asked on Stack Overflow Oct 27, 2018 by awfullyCold • edited Oct 27, 2018 by awfullyCold

1 Answer

0

There are few issues, firstly this

scanf("%d", param[i]);

should be

scanf("%d", &param[i]); /* need to provide address */

Also buildArray() prototype should be

void buildArray(int *);

instead of

void buildArray();

Also do..while condition is wrong, use param[i-1] instead of param[i] in condition part of the loop.

Sample Code

void buildArray(int param[]) {
        int i = 0;
        do {
                printf("Please enter number :");
                scanf("%d", &param[i]);
                printf("entered : %d\n", param[i]);
                i++; /* because of this, condition part should be param[i-1] != -1 */

        }while(param[i-1] != -1); /* it should be param[i-1] */
}
answered on Stack Overflow Oct 27, 2018 by Achal • edited Oct 27, 2018 by Achal

User contributions licensed under CC BY-SA 3.0