Declare, initialize and use pointers



  • a. Create variables
    Write a program that reads variables of different data types:
    Content:
    • Read successively 4 variables of the following data types.
    char
    int
    float
    double
    • Subsequently, the variables should be printed to the console.

    b. Create pointers
    Expand the created program to create pointers:
    Content:
    • Declare the pointers of these 4 types of data.
    • Assign the address of the variables defined in a. to these pointers.

    c. Use pointers
    Continue the program to use the pointers.
    Content:
    • Set the values of the variables to the values entered on console using only the pointers.
    • Print out the value of the variables also using only the pointers on the console.
    • In the end, assign all the pointers to NULL pointer, and print out the pointers again on the console. What will happen? Give your explanation using comment in the end of the program.

    Mein Ansatz

    #include <iostream>
    using namespace std;
    
    int main()
    {
        //Teilaufgabe a
        char a;
        int b;
        float c;
        double d;
    
        cout << "char: ";
        cin >> a;
        cout << "int: ";
        cin >> b;
        cout << "float: ";
        cin >> c;
        cout << "double: ";
        cin >> d;
    
        cout << "\nVariablen"<<endl;
        cout << "char: " <<a<< "\tint: " <<b<< "\tfloat: " <<c<< "\tdouble: " <<d<<endl;
    
        //Teilaufgabe b
        char *e = &a;
        int *f = &b;
        float *g = &c;
        double *h = &d;
    
        //Teilaufgabe c
        cout << "\nZeiger"<<endl;
        cout << "char: " <<*e<< "\tint: " <<*f<< "\tfloat: " <<*g<< "\tdouble: " <<*h<<endl;
    
        *e = NULL;
        *f = NULL;
        *g = NULL;
        *h = NULL;
    
        cout << "\nVariablen mit NULL"<<endl;
        cout << "char: " <<*e<< "\tint: " <<*f<< "\tfloat: " <<*g<< "\tdouble: " <<*h<<endl;
    
        //Da man pointer auf NULL setzt, wird auch der Wert auf NULL gesetzt, somit         kommt es zu den Werten 0 oder sogar bei char zu keinem Zeichen.
    
        return 0;
    }
    

    Ich hab soweit wie ich es konnte gemacht...nur bei der Teilaufgabe c bin ich mir unsicher... 😕


  • Mod

    a und b sind mehr oder weniger richtig. Wobei es nur eine Frage von Minuten sein wird, bis du bei der komischen Wahl deiner Bezeichner selber nicht mehr durchblicken wirst.

    c: Was genau ist deine Frage? Die Aufgabenstellung könnte wohl kaum detaillierter sein.
    -Es fehlt noch eine Wiederholung der Eingabe.
    -Du setzt nicht die Zeiger auf NULL, sondern das, worauf sie zeigen. Würdest du nullptr statt NULL benutzen, hätte der Compiler diesen Logikfehler gemeldet. Lass das Dereferenzieren sein! Es sollen Zeiger manipuliert werden.
    -Zeile 42 ist Unsinn. Hast du dir vermutlich anhand der Ausgabe zusammen gereimt, ist aber Blödsinn, da die Ausgabe aus dem vorhergehenden Logikfehler resultiert.

    PS: Das hier ist vielleicht was für dich:
    https://www.youtube.com/watch?v=i49_SNt4yfk



  • ich stelle mich gerade richtig dumm mit den pointern da...
    meinst du wohl eher
    e = NULL;
    f = NULL;
    g = NULL;
    h = NULL;



  • Oder halt gleich so

    e = nullptr;
    f = nullptr;
    g = nullptr;
    h = nullptr;
    

    P.S.: Hab grad den Anfang des Videos angeschaut, finds ne super süße Erklärung und würd ich mir auf jeden Fall anschauen!



  • #include <iostream>
    using namespace std;
    
    int main()
    {
        //Teilaufgabe a
        char a;
        int b;
        float c;
        double d;
    
        cout << "char: ";
        cin >> a;
        cout << "int: ";
        cin >> b;
        cout << "float: ";
        cin >> c;
        cout << "double: ";
        cin >> d;
    
        cout << "\nVariablen"<<endl;
        cout << "char: " <<a<< "\tint: " <<b<< "\tfloat: " <<c<< "\tdouble: " <<d<<endl;
    
        //Teilaufgabe b
        char *e = &a;
        int *f = &b;
        float *g = &c;
        double *h = &d;
    
        //Teilaufgabe c
        cout << "\nchar: ";
        cin >> *e;
        cout << "int: ";
        cin >> *f;
        cout << "float: ";
        cin >> *g;
        cout << "double: ";
        cin >> *h;
    
        cout << "\nZeiger"<<endl;
        cout << "char: " <<*e<< "\tint: " <<*f<< "\tfloat: " <<*g<< "\tdouble: " <<*h<<endl;
    
        e = nullptr;
        f = nullptr;
        g = nullptr;
        h = nullptr;
    
        cout << "\nPointer mit NULL"<<endl;
        cout << "char: " <<e<< "\tint: " <<f<< "\tfloat: " <<g<< "\tdouble: " <<h<<endl;
    
        return 0;
    }
    

    😕 😕


Anmelden zum Antworten