简体   繁体   中英

C enum Printing error?

In College we had to write a program with structs, enums and unions to display a date, like 1.September 2014. I had some problems with printing an enum. I found some solutions in this forum and finally I've got no console errors anymore. But Everytime I try to run my Program it crashes. Maybe someone of you knwos what's causing the crash:

#include <stdio.h>

    enum Monate{JANUAR = 1,
                FEBRUAR = 2,
                MAERZ = 3,
                APRIL = 4,
                MAI = 5,
                JUNI = 6,
                JULI = 7,
                AUGUST = 8,
                SEPTEMBER = 9,
                OKTOBER = 10,
                NOVEMBER = 11,
                DEZEMBER = 12 };

    union Monat{
        enum Monate alsMonat;
        char alsZahl;
        char alsString[10];
    };

    struct Datum {
        char tag;
        union Monat monat;
        short jahr;
    };


const char* welcherMonat(enum Monate meinMonat){
    switch(meinMonat){
            case JANUAR: return"Januar";
            break;

            case FEBRUAR: return"Februar";
            break;

            case MAERZ: return"Maerz";
            break;

            case APRIL: return"April";
            break;

            case MAI: return"Mai";
            break;

            case JUNI: return"Juni";
            break;

            case JULI: return"Juli";
            break;

            case AUGUST: return"August";
            break;

            case SEPTEMBER: return"September";
            break;

            case OKTOBER: return"Oktober";
            break;

            case NOVEMBER: return"November";
            break;

            case DEZEMBER: return"Dezember";
            break;
        }
}

void ausgabe(struct Datum *datum){
    char month[10]=" ";
    printf("Tag: %c\n",(*datum).tag);

    enum Monate monat2=(*datum).monat.alsMonat;
    printf("Monat: %s\n",welcherMonat(monat2));
    printf("Jahr: %d\n\n",(*datum).jahr);

}


int main(int argc, char** argv){

    struct Datum Geburtstag = {'3',AUGUST,1995};
    struct Datum Heute = {'3','12',2014};
    struct Datum Millenium = {'1',1,2000};

        ausgabe(&Geburtstag);
        ausgabe(&Heute);
        ausgabe(&Millenium);

    return 0;
}

Use GDB. It is the Gnu DeBugger. Compile your code with the -g option enabled, and it will include all of the proper debugging symbols. You can then use GDB to put a manual break in the program and step through it one line at a time, look at variables, and see exactly where it is crashing.

How can you have a multi-character char like this:

struct Datum Heute = {'3','12',2014};

Doesn't your compiler throw a warning for this? It should ideally

It seems like I overlooked the MultiCharacter warning... When I choose a one-character month it still freezes at the second print, when I want to print the date by using a char as month. The other two types of printing the month(by enum) work totally fine. Our task was to print three different dates by using all three kinds of month-printing.

I think I know the Problem:

enum Monate monat2=(*datum).monat.alsMonat;
printf("Monat: %s\n",welcherMonat(monat2));

when I give the function a char instead of an enum, "monat2" is empty. But how could I check whether I give him a char or an enum?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM