Woher kommmen die vielen nervigen \0?



  • Guten Abend.
    Irgendwie komme ich mit char[] nicht zurecht bin leider nur C++ Strings gewohnt.

    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    
    int main()
    {
        char dumm[10] = "123456789", sue[10]="123456789";
        char antwort[sizeof(dumm) + sizeof(sue) - 1];
    
        cout << "Antwort ist: " << sizeof(antwort);
        cout << "\n\n\n" << sizeof(antwort) << "\n";
    
        int anzahl = 0;
    
        for(anzahl;anzahl<sizeof(dumm);anzahl++){
    
            if(dumm[anzahl] == '\0'){
                break;
            }else{
                antwort[anzahl] = dumm[anzahl];
                cout << antwort[anzahl] << "\n";
            }
        }
    
        for(int dachs=0;dachs<sizeof(sue);dachs++){
            anzahl++;
    
            if(sue[dachs] == '\0'){
                break;
            }else{
                antwort[anzahl] = sue[dachs];
                cout << antwort[anzahl] << "\n";
            }
        }
    
        cout << "\n\n\n";
        antwort[19] = '\0';
    
        for(int sand=0;sand<sizeof(antwort);sand++){
            cout << "\n" << antwort[sand];
        }
    
        return 0;
    }
    

    Ausgabe:

    Antwort ist: 19

    19
    1
    2
    3
    4
    5
    6
    7
    8
    9
    1
    2
    3
    4
    5
    6
    7
    8
    9

    1
    2
    3
    4
    5
    6
    7
    8
    9

    1
    2
    3
    4
    5
    6
    7
    8
    9

    Also bei meiner überlegung dürfte der CString antwort an der 10 stelle kein '\0' haben da es ja durch

    if(dumm[anzahl] == '\0'){
                break;
    

    nie eingefügt werden sollte bei meiner Weltanschauung.



  • anzahl++ in Zeile 28 ist zu früh, würde ich meinen.



  • volkard schrieb:

    anzahl++ in Zeile 28 ist zu früh, würde ich meinen.

    Vielen dank hat funktioniert 🙂

    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    
    int main()
    {
        char dumm[10] = "123456789", sue[10]="123456789";
        char antwort[sizeof(dumm) + sizeof(sue) - 1];
    
        cout << "Antwort ist: " << sizeof(antwort);
        cout << "\n\n\n" << sizeof(antwort) << "\n";
    
        int anzahl = 0;
    
        for(anzahl;anzahl<sizeof(dumm);anzahl++){
    
            if(dumm[anzahl] == '\0'){
                break;
            }else{
                antwort[anzahl] = dumm[anzahl];
                cout << antwort[anzahl] << "\n";
            }
        }
    
        for(int dachs=0;dachs<sizeof(sue);dachs++){
    
            if(sue[dachs] == '\0'){
                break;
            }else{
                antwort[anzahl] = sue[dachs];
                cout << antwort[anzahl] << "\n";
            }
    
                    anzahl++;
        }
    
        cout << "\n\n\n";
        antwort[18] = '\0';
    
        for(int sand=0;sand<sizeof(antwort);sand++){
            cout << "\n" << antwort[sand];
        }
    
        return 0;
    }
    


  • Ich suche grad mal cout im C-Standard, scheint neu zu sein. 🙂


Anmelden zum Antworten