简体   繁体   English

我的代码在 VSCode 中运行,但不在 DevC 中运行

[英]My code runs in VSCode but does not run in DevC

#include <stdlib.h>
#include <stdio.h>

void arraydescending(int array[]){
    for (int j=0; j<9; j++){
        for (int i=0; i<8; i++)
        {
            if(array[i]<array[i+1]){
            int swapper = array[i+1];
            array[i+1]=array[i];
            array[i]=swapper;
            }
        }
    }
    for (int c=0; c<9; c++){
        printf("%d",array[c]);
    }
}

void arrayreverse(int array[])
{
    for(int i = 0; i<4; i++)
    {
        int swapper = array[i];
        array[i] = array[8-i];
        array[8-i] = swapper;
    }
    for (int c=0; c<9; c++){
        printf("%d",array[c]);
    }
}

int main()
{
    int choice;
    printf("Please enter your choice:");
    scanf("%d", &choice);
    if(choice == 1)
    {
        int mynumberarray[9] = {1,1,0,2,0,0,0,4,7};
        int choice_2;
        printf("Write 1 for reverse order, write 2 for descending order:");
        scanf("%d", &choice_2);
        if(choice_2 == 1)
        {
            arrayreverse(mynumberarray);
        }
        else if(choice_2 == 2)
        {
            arraydescending(mynumberarray);
        }
        else
        {
            printf("Invalid choice");
        }
    }
    else if(choice == 2){
        int userarray[9];
        char * user_entry;
        printf("Please enter your school no (9 digits):");
        scanf("%s",user_entry);
        for(int i = 0; i < 9; i++)
        {
            userarray[i] = user_entry[i] - '0';
        }
        int choice_2;
        printf("Write 1 for reverse order, write 2 for descending order:");
        scanf("%d", &choice_2);
        if(choice_2 == 1)
        {
            arrayreverse(userarray);
        }
        else if(choice_2 == 2)
        {
            arraydescending(userarray);
        }
        else
        {
            printf("Invalid choice");
        }
    }
    else
    {
        printf("Invalid choice");
    }
    return 0;
}

This code runs correctly when I compiled it with gcc -std=c99;当我使用 gcc -std=c99; 编译它时,此代码运行正确but my friend has DevC 5.11 version can compile the code but it doesn't run correctly in his DevC (It exits the program in the second scanf).但是我的朋友有 DevC 5.11 版本可以编译代码,但它在他的 DevC 中无法正确运行(它在第二个 scanf 中退出程序)。 Both compiles but why it does not run in DevC 5.11 with the compiler gcc 4.9.2?两者都可以编译,但为什么它不能在 DevC 5.11 中使用编译器 gcc 4.9.2 运行? I am waiting for your suggestions because I didn't understand the reason behind it, my code looks like it has not any mistakes.我正在等待您的建议,因为我不明白它背后的原因,我的代码看起来没有任何错误。

Your program has undefined behavior at least because in this code snippet您的程序至少有未定义的行为,因为在此代码段中

    char * user_entry;
    printf("Please enter your school no (9 digits):");
    scanf("%s",user_entry)

you are using the uninitialized pointer user_entry that has an indeterminate value.您正在使用具有不确定值的未初始化指针user_entry You need to declare a large enough character array where you are going to read a string of digits.您需要声明一个足够大的字符数组,您将在其中读取一串数字。 Do not forget to reserve in the array a space for the terminating zero character of the read string.不要忘记在数组中为读取字符串的终止零字符保留一个空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 c 代码从 VSCode 终端运行时崩溃,但从 gnome 终端运行时运行良好? - Why does my c code crash when run from VSCode terminal, but runs fine when run from gnome terminal? 为什么以下 c 代码在 turbo c 中工作而不是在 devc++ 中工作? - Why does the following c code work in turbo c and not in devc++? 为什么我的C代码运行缓慢? - Why does my C code run slow? 在DEvC ++和Ideone上有效的代码,但在竞赛网站上无效 - Code working on DEvC++ and Ideone but not on competetion website 此代码在DevC ++中编译没有问题,但Visual Studio 2008吐出这些警告并拒绝编译。 我的错误在哪里? - This code compiles in DevC++without problems but Visual Studio 2008 spits these warnings and refuses to compile. Where is my mistake? 我的代码运行良好,测试 A 或测试 B 被注释掉,但两者都运行程序崩溃 - My code runs fine with test A or test B commented out, but both are run the program crashes 当我运行我的代码时,我得到了一个段错误,但是当调试器运行它时,它说没有问题 - When I run my code, i get a segfault, but when the debugger runs it, it says there is no issue 我的代码运行没有错误,但它只运行了一段代码(C语言) - My code runs with no errors but it runs only a section of the code (C Language) CS50 Pset 2. 我的crack.c 代码编译并运行但不打印任何内容 - CS50 Pset 2. My crack.c code compiles and runs but does not print anything 为什么 free() 存在时会导致我的代码出错,但不存在时一切运行良好? - Why does free() cause an error in my code when it's there, but everything runs well when it is not there?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM