Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c - 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; 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). Both compiles but why it does not run in DevC 5.11 with the compiler 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...