**#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);
}**


你好,我想从用户那里得到输入。用户何时输入-1号,我想打破这个循环。但是我得到一些错误代码:““ HelloC.exe中0x53918F0E(ucrtbased.dll)的未处理异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。”

我的错在哪里?
当我尝试将函数类型从void更改为int时,得到相同的错误。

最佳答案

问题很少,首先是

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


应该

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


另外buildArray()原型应该是

void buildArray(int *);


代替

void buildArray();


同样,do..while条件是错误的,请在循环的条件部分中使用param[i-1]代替param[i]

样例代码

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] */
}

关于c - 0x53918F0E(ucrtbased.dll)的未处理异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53024390/

10-12 13:35