Inkompatible Typen - Liegt es daran, dass ein Array bereits ein Zeiger ist?

Im folgenden Code erstelle ich ein Objekt, das auf der Buchstruktur basiert, und damit es mehrere "Bücher" enthält, setze ich ein Array (dh das Objekt, das definiert / initiiert wird). Wenn ich jedoch meine Zeigerkenntnisse teste (die Übung hilft) und versuche, einen Zeiger zu erstellen, der auf das erstellte Objekt verweist, erhalte ich den Fehler:

C: \ Users \ Justin \ Desktop \ Project \ wassuip \ main.cpp | 18 | Fehler: Inkompatible Typen bei der Zuweisung von Büchern'zu' Bücher * [4] '| *

Darf ich fragen, ob dies daran liegt, dass das Objekt book_arr [] bereits als Zeiger betrachtet wird, da es sich um ein Array handelt? Vielen Dank (neu in C ++ und möchte nur überprüfen).

#include <iostream>
#include <vector>
#include <sstream>

#define NUM 4

using namespace std;

struct books {
    float price;
    string name;
    int rating;
} book_arr[NUM];

int main()
{
    books *ptr[NUM];
    ptr = &book_arr[NUM];

    string str;

    for(int i = 0; i < NUM; i++){
        cout << "Enter book name: " << endl;
        cin >> ptr[i]->name;
        cout << "Enter book price: " << endl;
        cin >> str;
        stringstream(str) << ptr[i]->price;
        cout << "Enter book rating: " << endl;
        cin >> str;
        stringstream(str) << ptr[i]->rating;
    }

    return 0;
}

*NEUER CODE NACH ANTWORTEN (KEINE FEHLER) *

#include <iostream>
#include <vector>
#include <sstream>

#define NUM 4

using namespace std;

/* structures */
struct books {
    float price;
    string name;
    int rating;
} book[NUM];

/* prototypes */
void printbooks(books book[NUM]);

int main()
{
    string str;

    books *ptr = book;

    for(int i = 0; i < NUM; i++){
        cout << "Enter book name: " << endl;
        cin >> ptr[i].name;
        cout << "Enter book price: " << endl;
        cin >> str;
        stringstream(str) << ptr[i].price;
        cout << "Enter book rating: " << endl;
        cin >> str;
        stringstream(str) << ptr[i].rating;
    }

    return 0;
}

void printbooks(books book[NUM]){
    for(int i = 0; i < NUM; i++){
        cout << "Title: \t" << book[i].name << endl;
        cout << "Price: \t$" << book[i].price << endl;
        cout << "Racing: \t" << book[i].rating << endl;
    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage